Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Arrow functions, introduced in ECMAScript 6, provide a concise syntax for writing function expressions in JavaScript. They allow you to define anonymous functions using a more compact syntax, omitting the `function` keyword. Arrow functions automatically bind `this` to the surrounding code's context, making them useful for writing shorter, more readable code, especially when working with callbacks and higher-order functions. In this article, we will discuss this in detail.
Functions in JavaScript
JavaScript is based on functional programming. Hence, functions are fundamental building blocks of JavaScript. A function is a block of code that performs a specific task. Let's see an example of a regular function.
// Declaring and calling a regular function
const hello = function () {
console.log("Hey Ninjas!");
};
hello();
You can also try this code with Online Javascript Compiler
Arrow functions in JavaScript are a concise alternative way to write regular functions in JavaScript. They are also called "fat arrow functions" as they use a fat arrow (=>). It got its name arrow functions due to using a fat arrow.
In arrow functions, instead of the function keyword, a fat arrow is used. Let's see how the above regular function can be written using the fat arrow function.
// Declaring and calling an arrow function
const hello = () => {
console.log("Hey Ninjas!");
}
hello();
You can also try this code with Online Javascript Compiler
Inside a regular function, the ‘this’ keyword refers to the function where it is called. However, when this keyword is used with the arrow function, it refers to its parent scope. This happens because the arrow function doesn't define its own execution context and does not have its own "this".
Let's look at an example for a better understanding.
// Regular function
function Student() {
this.name = 'Sapna',
this.introduce = function () {
// Here this is accessible
console.log(this.name);
function innerIntroduceFunc() {
// Here this refers to the global object
console.log(this.name);
}
innerIntroduceFunc();
}
}
let s = new Student();
s.introduce();
You can also try this code with Online Javascript Compiler
Here, this.name inside this.introduce() is accessible because this.introduce() is the method of an object.
However, innerIntroduceFunc() is a normal function, and this.name is not accessible because this refers to the global object. Hence, this.name inside the innerFunc() function gives undefined.
// Arrow function
function Student() {
this.name = 'Sapna',
this.introduce= function () {
console.log(this.name);
// Keyword ‘this’ refers to parent scope
let innerIntroduceFunc = () => {
console.log(this.name);
}
innerIntroduceFunc();
}
}
const s = new Student();
s.introduce();
You can also try this code with Online Javascript Compiler
Here, the innerIntroduceFunc() is defined using the arrow function. So, this refers to the parent's scope.
Argument Binding
Regular functions in JavaScript have arguments binding. Due to this, we can access the arguments passed using the arguments keyword.
However, arrow functions in JavaScript do not have arguments binding. So, when we try to access an argument using the arrow function in JavaScript, it gives us an error. For example,
let func = () => {
console.log(arguments);
}
func(1,2,3);
You can also try this code with Online Javascript Compiler
An arrow function in JavaScript is a concise way to write function expressions, using the => syntax, which simplifies function scoping and syntax.
What does the => mean in JavaScript?
The => symbol in JavaScript defines an arrow function, creating a function with a shorter syntax and lexically binding this value.
When should I use arrow functions in JavaScript?
Use arrow functions in JavaScript for concise function expressions, when you need lexical scoping of this, and where function hoisting is not required.
Conclusion
This article discusses the topic of Arrow functions in JavaScript. In detail, we have seen the definition of arrow functions, this keyword, argument binding, promises and callbacks, Object literals, advantages and limitations.
We hope this blog has helped you enhance your knowledge of Arrow functions in JavaScript. If you want to learn more, then check out our articles.
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundles for placement preparations.
However, you may consider our paid courses to give your career an edge over others!