Arrow Functions in JavaScript
0 0
Read Time:2 Minute, 15 Second

Array Functions in JavaScript

Hello, Flocks👋

In this post, you will learn about Arrow Functions in Javascript with the help of examples.

What Are Arrow Functions?

Arrow Functions allow us to write shorter and cleaner function syntax as compared to regular functions.

for example:

// Regular Function
let abc = function(a,b){
 return a * b;
}

//Using Arrow Function
let ABC = (a, b) => a * b;

General Syntax:

Let someFunction = (arg1, arg2, ..., argN) => {
 Statement(s)
}

If the Function has only one statement, and the statement returns a value then you can remove the brackets and the return keyword :

Let someFunction = (arg1, arg2, ..., argN) => expression

Examples of Arrow Functions:

1. Arrow Function With No Argument

If a function Doesn’t take any argument then you should use empty parenthesis.

For example:

Let someFunction = () => console.log("Function without argument");
someFunction(); // Function without argument

2. Arrow Function With One Argument

If a Function has Only one argument then you can omit the parenthesis.

For example:

var abc = "Function with one argument" 
Let someFunction = abc => console.log(abc); 
someFunction(); // Function with one argument

3. Arrow Function as an Expression

You can also dynamically create a function and use it as an expression.

For example:

let age = 10;
let SomeAge = (age < 18) ? () => console.log("Child") : () => console.log("Adult");

SomeAge() // Child

this” With Arrow Function

Inside a regular function, the “this” keyword refers to the function where it is called.
However, this is not associated with the arrow function. An arrow function does not have it’s own “this“. so whenever you call this. it refers to its parent scope.

For example:

function DemoFunction(){
    this.count = 10;
    this.CallCount = function (){
        console.log(this.count);
        let innerFunction = () => {
            console.log(this.count);
        }
        innerFunction();
    }
}

const Func = new DemoFunction();
Func.CallCount();

/*Output:*/

//10
//10

Here, the “innerFunction()” function is defined using the arrow function and inside the arrow function, “this refers to the parent’s scope. Hence, “this.count” gives 10.

Note: Avoid doing these things with arrow functions:

  1. You should not use arrow functions to create methods inside objects.
  2. You cannot use an arrow function as a constructor.

♥️ If you find it useful, please express your appreciation by giving it a like! (P.S. -It won’t cost anything don’t worry just go for it 😉)

✍️ Feel free to comment with your thoughts and queries (Trust me it motivates me a lot 💎)

📥 Don’t forget to Bookmark it for later use (Or else you’ll miss it 🥲)

📲 Also share it with your friends and colleagues out there (Let’s help each other grow 👊🏻)

Do Follow Us on:

Facebook.com

Twitter.com

Also Read: Recursion in JavaScript

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

By Akash Kothari

I am passionate about my work. Because I love what I do, I have a steady source of motivation that drives me to do my best. In my last job, this passion led me to challenge myself daily and learn new skills that helped me to do better work

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

51 thoughts on “Arrow Functions in JavaScript

Leave a Reply

Discover more from TechDevil

Subscribe now to keep reading and get access to the full archive.

Continue reading