Tutorial by Examples

In JavaScript, functions may be anonymously defined using the "arrow" (=>) syntax, which is sometimes referred to as a lambda expression due to Common Lisp similarities. The simplest form of an arrow function has its arguments on the left side of => and the return value on the right...
Arrow functions are lexically scoped; this means that their this Binding is bound to the context of the surrounding scope. That is to say, whatever this refers to can be preserved by using an arrow function. Take a look at the following example. The class Cow has a method that allows for it to pr...
Arrow functions do not expose an arguments object; therefore, arguments would simply refer to a variable in the current scope. const arguments = [true]; const foo = x => console.log(arguments[0]); foo(false); // -> true Due to this, arrow functions are also not aware of their caller/ca...
Arrow functions may implicitly return values by simply omitting the curly braces that traditionally wrap a function's body if their body only contains a single expression. const foo = x => x + 1; foo(1); // -> 2 When using implicit returns, object literals must be wrapped in parenthesis s...
Arrow functions can behave very similar to classic functions in that you may explicitly return a value from them using the return keyword; simply wrap your function's body in curly braces, and return a value: const foo = x => { return x + 1; } foo(1); // -> 2
Arrow functions will throw a TypeError when used with the new keyword. const foo = function () { return 'foo'; } const a = new foo(); const bar = () => { return 'bar'; } const b = new bar(); // -> Uncaught TypeError: bar is not a constructor...

Page 1 of 1