6

What is arrow function in Javascript

 1 year ago
source link: https://www.laravelcode.com/post/what-is-arrow-function-in-javascript
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

What is arrow function in Javascript

  425 views

  10 months ago

Javascript

An arrow function is short function expression, used to replace with regular function in low complexity. Arrow functions were introduced to JavaScript in ES6.

The intention to create arrow function is to shorten function code. Arrow function has its advantage and disadvanatges comparing with normal function.

First of all, lets see how normal function looks in the examples:

// normal function to sum 2 numbers
function addition(a + b) {
    return a + b;
}

Now lets see how it is written in arrow function. In arrow function, there is no function keyword. 

var addition = (a + b) => {
    return a + b;
}

Even, if function has only one statement and it return a value, You don't even need to write return keyword.

var addition = (a + b) => a + b;

If there is only one parameter in the function, brackets can be removed.

var getDouble = a => a * 2;

There is limitation for arrow functions:

Arrow function doesn't have own bindings of this keyword.
Arrow function not be used as constructors.
Arrow functions are harder to read compare to normal functions.

However arrow functions are best to apply on callbacks as arrow functions are anonymous. Arrow functions are also good in methods like map or reduce.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK