Hey Ninjas! You must have heard the term JavaScript. So, once you start coding in JavaScript, you will realize that functions play a vital role in JavaScript. Functions can be declared using the classical regular functions way or with the help of arrow functions.
Our today's topic for this blog is Arrow functions in JavaScript. Let's take a closer look at the Arrow functions in JavaScript. If you are new to JavaScript, it is suggested to go through the JavaScript functions, and you can also refer to our JavaScript-guided path.
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();
Output:
Arrow Functions in JavaScript
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();
Output:
JavaScript does not allow a line break between the parameter definition and the fat arrow in an arrow function.
If an arrow function doesn't take any argument, then use empty parentheses. For example,
// Arrow functions with no arguments
const result = () => 20;
console.log(result());
Output:
Note: The above code is the same as,
// Arrow functions with no arguments
const result = () => {
return 20;
}
console.log(result());
Output:
Passing One Argument
We can omit the parentheses if an arrow function has only one argument. For example,
// Arrow function with one argument
const result = num => num + 5;
console.log(result(10));
Output:
Passing Multiple Arguments
We cannot omit the parentheses if an arrow function has more than one argument. For example,
// Arrow function with multiple arguments
const result = (num1, num2) => num1 + num2;
console.log(result(10, 5));
Output:
Multiline Arrow Function
If an arrow function has multiple statements in the code block, we cannot omit the curly braces and return the keyword. For example,
// Multiline arrow function
const result = (num1,num2) => {
let total = num1 + num2;
return total;
}
console.log(result(10, 5));
Output:
Using this keyword
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();
Output:
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();
Output:
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);
Output:
To solve this issue, we can make use of the spread operator.
let func = (...arguments) => {
console.log(arguments);
}
func(1,2,3);
Output:
Promises and Callbacks
While writing promises and callbacks, arrow functions provide a better syntax. For example,
Return can be omitted when there are no curly brackets. This is because arrow functions use implicit returns when written without curly brackets. In contrast, we have to use return explicitly when there are curly brackets.
Does an arrow function have a prototype property?
The function has the prototype property when a function is defined by the regular method using a function keyword. However, arrow functions in JavaScript don't have the prototype property.
Where are arrow functions often used?
Arrow functions are frequently used in promise chaining, callback chaining, array methods, and situations where anonymous functions would be useful.
How to make arrow functions in JavaScript asynchronous?
We can make an arrow function asynchronous by using async/await.
Why can arrow functions not be used as generators?
We cannot use the yield keyword within an arrow function's body. Hence, arrow functions cannot be used as generators.
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!
Happy Learning!
Live masterclass
Land an SDE Role at Google: Master DSA the smart way
by Saurav Prateek, SDE2@Google
19 Sep, 2024
01:30 PM
Become a Data Analyst at MAANG: Learn PowerBI for Data visualization
by Megna Roy, Senior Data Analyst @Amazon
17 Sep, 2024
01:30 PM
Learn Advanced Excel: Ace Data Analytics Interview
by Megna Roy, Data Analyst @ Amazon
15 Sep, 2024
06:30 AM
Land an SDE Role at Google: Master DSA the smart way
by Saurav Prateek, SDE2@Google
19 Sep, 2024
01:30 PM
Become a Data Analyst at MAANG: Learn PowerBI for Data visualization