Three ways to define a function
A function packages steps under a name so you can run them many times with different inputs. JavaScript has three syntaxes: the declaration (hoisted, has a name, has its own this), the expression (a function stored in a variable) and the arrow function (short, no own this, the default for callbacks). All three are values — you can pass them around like numbers.
You should see
Hello, Ada!
HI!
16 5 { name: 'Ada', age: 36 } undefined
10 [ 1, 4, 9 ] function
logging a
undefinedisEven as an arrow function, then use it with filter on [1, 2, 3, 4, 5, 6].Arrow returning an object: () => { name: "Ada" }
const makeUser = name => { name: name }
console.log(makeUser("Ada"))undefinedNo error, wrong result. Braces after => start a block, not an object. Inside it, name: name is parsed as a label followed by an expression, and the function returns nothing.
Wrap the object literal in parentheses.
const makeUser = name => ({ name })
console.log(makeUser("Ada")) // { name: 'Ada' }greet() {}, because they need this. The classic function expression is now rare.