DEV Community

Megalraja
Megalraja

Posted on

JavaScript Arrow Functions

Today I learned about arrow functions in JavaScript.

An arrow function is a shorter way to write a function.

Normal function

function add(a, b) {
    return a + b;
}
Enter fullscreen mode Exit fullscreen mode

Arrow function

const add = (a, b) => a + b;
Enter fullscreen mode Exit fullscreen mode

Here:

const add → stores the function
(a, b) → parameters
=> → arrow function syntax
a + b → returned result

Example:

console.log(add(10, 20));

Output:

30
Enter fullscreen mode Exit fullscreen mode

The shorter syntax makes simple functions easier to write and read.

Top comments (0)