Skip to content

Arrow Functions (JavaScript)

With this method, we can define anonymous function expressions in a short way.


var collection = (x, y) => x + y;

Output:  10

Same as above example:

var collection = function (x, y) {
            return x + y;

As seen, the function expression is defined with a shorter method.

The example (x, y) declares the parameters of the function. The arrow in => indicates the function body. The x + y after the octane indicates the value that the function returns. There is no return statement here.

If there is only one parameter, there is no need to use parentheses in the parameter section.


var pow = x => x * x;

Output:  25

If there are no parameters, it is necessary to turn on and off the brackets.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: