Arrow operator in ES6 of JavaScript
Last Updated :
19 Jun, 2023
ES6 has come with various advantages and one of them is the arrow operator. It has reduced the function defining code size so it is one of the trending questions asked in the interview. Let us have a deeper dive into the arrow operator's functioning.
Syntax: In ES5 a function is defined by the following syntax:
function functionName(arg1, arg2….) {
// body of function
}
But in ES6 a function is defined using an arrow operator and whose syntax is like this:
let functionName = (arg1, arg2 ….) => {
// body of function
}
Advantages of Arrow Operator:
1) Reduces Code Size: As we have replaced the traditional function syntax with the corresponding arrow operator syntax so the size of the code is reduced and we have to write less amount of code for the same work.
2) Drop the Function braces for one line Functions: We can drop the braces of the function in the arrow operator declaration for example:
Example 1: This example shows the basic use of the arrow functions in Javascript.
javascript
//ES5 VERSION
let setSize = (sz) =>
size = sz;
//sets size value to the passed value
setSize(10);
console.log(size);
Output:
10
This can also be written as:
Example 2: This example shows the basic use of the arrow functions in Javascript.
javascript
//ES6 Version
//Do not need to put curly braces for one line functions
setDoubleSize = (sz) => size = 2 * sz;
//Sets value of size equivalent to double of
//passed argument in setDoubleSize function
setDoubleSize(35);
console.log(size);
Output:
70
3) No need to define return statement in one line Functions: In ES5 you have to define the return statement in the functions and if in ES6 we do not define the return statement then ES6 automatically returns the value whenever a given function is called. This will be clear by the following example: ES5 version of one bit left shifting function is as follows :
function leftShift(value) {
return value / 2;
}
While in ES6 the following function can be written as follows:
// Always try to use let keyword for the declaration
// of any variable instead of using var keyword
// in order to avoid unnecessary hoisting and closures
// as well as mismatching values of variables...
let leftShift = (value) => value / 2;
Example 3: In this example, we will return a value in arrow functions without using the return statement.
javascript
//ES5 VERSION
function leftShiftES5(value) {
return value / 2;
}
//ES6 VERSION
let leftShiftES6 = (value) => value / 2;
let a = 10, b = 10;
console.log('values before left shift');
console.log('a : ' + a + ' b : ' + b);
//Both of the function should give same output
a = leftShiftES5(a);
b = leftShiftES6(b);
console.log('values after left shift');
console.log('a : ' + a + ' b : ' + b);
Output:
values before left shift
a : 10 b : 10
values after left shift
a : 5 b : 5
4) Lexically bind the context: Arrow operator syntax lexically binds the context so this refers to the originating context. It means that it uses this from the arrow functions.
Example: Let us consider a class having an array of ages and if the age<18 we will push them into the child queue. In ES5 you have to do this as follows :
javascript
this.age.forEach(function (age) {
if (age < 18)
this.child.push(age);
}.bind(this));
In ES6 this can be done as follows :
JavaScript
this.age.forEach((age) => {
if (age < 18)
this.child.push(age);
});
So we do not have to bind it implicitly and this is done automatically by arrow functions. So we have seen arrow function makes the function writing less complex and reduces the number of lines so it is being used by questions the developer and also it is one of the most trending questions asked during interviews.
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
JavaScript Interview Questions and Answers JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. Both front-end and back-end developers need to have a strong command of JavaScript, as
15+ min read
Introduction to JavaScript JavaScript is a versatile, dynamically typed programming language used for interactive web applications, supporting both client-side and server-side development, and integrating seamlessly with HTML, CSS, and a rich standard library.JavaScript is a single-threaded language that executes one task at
7 min read
JSON Web Token (JWT) A JSON Web Token (JWT) is a standard used to securely transmit information between a client (like a frontend application) and a server (the backend). It is commonly used to verify users' identities, authenticate them, and ensure safe communication between the two. JWTs are mainly used in web apps an
7 min read
Frontend Developer Interview Questions and Answers Frontend development is an important part of web applications, and it is used to build dynamic and user-friendly web applications with an interactive user interface (UI). Many companies are hiring skilled Frontend developers with expertise in HTML, CSS, JavaScript, and modern frameworks and librarie
15+ min read
JavaScript Coding Questions and Answers JavaScript is the most commonly used interpreted, and scripted Programming language. It is used to make web pages, mobile applications, web servers, and other platforms. Developed in 1995 by Brendan Eich. Developers should have a solid command over this because many job roles need proficiency in Jav
15+ min read
Functions in JavaScript Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.JavaScriptfunction sum(x, y) { return x + y; } console.log(sum(6, 9));Output15 Function Syntax and W
5 min read
Top 95+ Javascript Projects For 2025 JavaScript is a lightweight, cross-platform programming language that powers dynamic and interactive web content. From real-time updates to interactive maps and animations, JavaScript brings web pages to life.Here, we provided 95+ JavaScript projects with source code and ideas to provide hands-on ex
4 min read
JavaScript Exercises, Practice Questions and Solutions JavaScript Exercise covers interactive quizzes, tracks progress, and enhances coding skills with our engaging portal. Ideal for beginners and experienced developers, Level up your JavaScript proficiency at your own pace. Start coding now! A step-by-step JavaScript practice guide for beginner to adva
3 min read
HTML DOM (Document Object Model) The HTML DOM (Document Object Model) is a programming interface that represents the structure of a web page in a way that programming languages like JavaScript can understand and manipulate. Think of it as a tree of objects where each part of your HTML document (elements, attributes, text) is repres
6 min read