Arrow functions in javascript

Introduction to arrow functions

Arrow functions are introduced in ecmaScript 6 or ES6. It is a scripting-language specification standardized for JavaScript.

Read more about functional programming using arrays in ES6.

Basic Syntax

// without arrow function

learn = function() {
    return 'from The CS blog';
});
// with arrow function

learn = () => {
    return 'from The CS blog';
});

Writing in a single line

It can also be written in a single line, if there is only one statement. No need to add a return statement here.

learn = () => 'from The CS blog';

Parameter passing

Parameters can be passed like this.

// Parameter passing

learn = (name) => {
    return `from ${name}`;    // using back-tick operator here
});

learn('The CS blog');

What is this ?

Main difference between using a regular function keyword and an arrow function is the handling of this is different in both methods.

In regular functions, every new function used to define there own this value. But arrow functions does not have their own this value. You can read more about this here.


Happy learning 🙂

Leave a Reply