Arrow functions

// ES5 example var sayHello = function(name) { return 'Hello ' + name + '!'; }; // Using ES6: // Arrow function (The one liner) var sayHello = name => `Hello ${name}!`; sayHello('World'); // Hello World // With block in case multiple statements are involved var sayHello = name => { // Say hello to world in case no name provided name = name || 'World'; return `Hello ${name}!`; }; sayHello(); // Hello World! sayHello('Jim'); // Hello Jim! // Without parameters var sayHello = () => { return 'Hello World!'; }; sayHello(); // Hello World! // With multiple parameters var greet = (greeting, name) => `${greeting} ${name}!`; // or with block var greet = (greeting, name) => { return `${greeting} ${name}!`; }; greet('Hello', 'World'); // Hello World
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

this in an arrow function is NOT a variable.
this arrow function is lexical.
- What that mean is, when we define an arrow function, this value will be assigned based on where the arrow function was defined, rather than what it will be assigned later when the application was running.
- this in arrow function works more like a constant, rather than a variable.
- The value of a constant was being deci

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.