Arrow functions

Arrow functions make function declaration much more compact. The traditional way of defining a function in JavaScript is using a function keyword. The following function gets one argument and just returns the argument value:

function hello(greeting) {
return greeting;
}

By using the ES6 arrow function, the function looks as follows:

const hello = greeting => { greeting }

// function call
hello('Hello World'); // returns Hello World

If you have more than one argument, you have to wrap the arguments in parentheses and separate arguments with a comma. The following function gets two parameters and returns the sum of the parameters. If the function body is an expression, you don't need to use the return keyword. The expression is always implicitly returned from the function:

const calcSum = (x, y) => { x + y }

// function call
calcSum(2, 3); // returns 5

If the function doesn't have any arguments, the syntax is the following:

() => { ... }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.252.204