ARROW FUNCTION

Arrow functions are a new type of anonymous functions, introduced in ES6, they have a compact and easily readable syntax. Arrow functions are particularly suitable as callbacks.

JSArrow functions allow us to write shorter function syntax:

let myFunction = (a, b) => a * b;

Before the arrow functions we had:

hellofunction() {
  return “Hello World!”;
}

With the arrow functions we have:

hello = () => {
  return “Hello World!”;
}

It gets shorter! If the function has only one statement and the statement returns a value, you can remove the parentheses and the return keyword:

hello = () => “Hello World!”;

Note: This only works if the function has only one statement. If you have parameters, you pass them in brackets:

hello = (val) => “Hello ” + val;

Also, if you only have one parameter, you can skip the brackets too:

hello = val => “Hello ” + val;

Copy to Clipboard

LINKS TO PREVIOUS POST

THE JAVASCRIPT LANGUAGE

LINK TO THE CODE ON GITHUB

GITHUB