KandZ – Tuts

We like to help…!

JavaScript 15 🧬 Arrow functions

a - arrow functions introduction
Arrow functions in JavaScript provide a concise syntax for writing functions
They are part of the ECMAScript 2015 (ES6) specification.
They offer several advantages such as more compact syntax, lexical this binding, and no need for the function keyword.
They don't have their own bindings to this, arguments, or super, and should not be used as methods.
They cannot be used as constructors and cannot use yield. They cannot be created as generator functions
syntax:

const functionName = (parameters) = {
// function body
};

The parantheses around parameters can only be omitted if there is a single parameter

const squareRoot = a = {
return a*a
};

Curly braces can only be omitted if the function returns an expression

const squareRoot = a = return a*a


b - Arrow function examples
Example 1 → concise syntax, omit parentheses
Example 2 → concise syntax, omit curly braces
Example 3 → Basic syntax
Example 4 → with multiple statements
Example 5 → `this` correctly refers to the person object
Example 6 → arrow function in array method
Example 7 → Arrow functions cannot be used as constructors. TypeError: Foo is not a constructor

Leave a Reply