An overview of essential syntax in JavaScript (ES6+).
Arrow functions provide a concise syntax to express functions. This is nice because most of what we do in JavaScript is write functions, so a clear, concise way to express functions is very convenient.
Arrow functions have the following advantages:
- They're expressed using an arrow,
=>instead of thefunctionkeyword. - If your function body simply returns the result of a single expression, you may use an implicit return. Simply omit the function body braces (
{}), and thereturnkeyword. E.g.,const isTrue = (value) => value === true;is equivalent toconst isTrue = value => { return value === true; }; - If your function takes only one parameter, and your function signature does not use an operator such as the rest operator (
...), or the default value operator (=), you can omit the parenthesis, e.g.,const isTrue = (value) => value === true;is equivalent toconst isTrue = value => value === true;
Write an arrow function that returns true if a number is even, false otherwise. It should have the following signature:
isEven(n) => BooleanHint: A number is even if it is evenly divisible by 2, meaning that the remainder after dividing will be 0. The modulo operator can be used to find the remainder of a division. e.g., 4 % 2 === 0, so 4 is even. 5 % 2 === 1, so 5 is odd.
Arrow functions do not behave the same way as functions declared with the function keyword. They have the following differences:
- Arrow functions don't have an
argumentsobject. ES6 provides the the rest operator (...), which gathers zero or more arguments together into an array. This is considered to be a better alternative to theargumentsobject, which was commonly used to access arguments prior to ES6. Since the arguments object doesn't have the capabilities of a real array, most users copy its contents to a real array before attempting to use them. Because of the rest operator, this is no longer necessary in ES6. - Arrow functions don't have their own
thiscontext. In method calls,thisis a special variable used to operate on an object to which the method is bound. In arrow functions,thisalways refers to the object bound tothisin the lexical scope. Since arrow functions don't have their own slot for athisbinding, you also can't rebindthisto an arrow function using theFunction.prototype.bind()method. - Arrow functions can't be used as constructor functions. If you try to call use an arrow function with the
newkeyword, it will throw an error. For example, if you try to usenewwith an arrow function calledFoo, V8 will throw the following:TypeError: Foo is not a constructor. - Because you can't use arrow functions as constructors, arrow functions will never have their own bindings for
super()ornew.target, which are only useful in the context of constructor functions.