Skip to content
Advertisements

Arrow Functions (JavaScript)

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

Example:

var collection = (x, y) => x + y;
        document.write(collection(3,7))

Output:  10

Same as above example:

var collection = function (x, y) {
            return x + y;
        };
        document.write(collection(3,7))

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.

Example:

var pow = x => x * x;
document.write(pow(5));

Output:  25

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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: