Saturday, March 30, 2024

What is JavaScript Arrow Function?

Arrow functions allow us to write shorter function syntax.

Let us understand this with an example:

Before Arrow function:

function hello() {

  return "Hello World!";

}

console.log(hello());

Output: Hello World!

After Arrow function:

hello = () => {

  return "Hello World!";

}

console.log(hello());

Output: Hello World!

Let us understand with one more example:

Before Arrow function:

let x = myFunction(1, 2);

function myFunction(a, b) {

// Function returns the sum of a and b

  return a + b;

}

console.log('before arrow function ==> '+x);

Output:

before arrow function ==> 3

After Arrow function:

myFunction = (a,b) => {

  return a + b;

}

console.log('after arrow function ==> '+myFunction(2,3));

Output:

after arrow function ==> 5

No comments:

Post a Comment