Anonymous functions

I am really confused about this chapter in the Javascript tutorial: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/use-arrow-functions-to-write-concise-anonymous-functions

In particular, is const myFunc also a function? Why is it labeled as const instead of function? Also, in the last case,

const myFunc = () => “value”;

what is the difference between this and assigning const myFunc = “value”? Why are we passing in an anonymous function instead?

1 Like

that is ES6 syntax of arrow functions which is a simpler more concise syntax to create functions

const myFunc = () => {
  // do something here
};

is the same as:

function myFunc() {
  // do something here
};

in this particular case it is the way to define your functions using ES6 syntax; but you can read up on the const keyword here javascript.info

As the challenge explains ES6 arrow functions allows you to omit the keyword return and the brackets surrounding the code to make your code shorter to write for one-line statements so this

function myFunc() {
  return "value"
};

can be written in ES6 syntax like this:

const myFunc = () => "value"

See javascript.info for more info on arrow functions.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.