JavaScript Interview Question #20

What is an arrow function in JavaScript?

JavaScript Fundamentals, Types, Scope & Functions Junior Beginner

Quick Interview Answer

An arrow function is a compact function syntax introduced in ES6. It is especially convenient for callbacks and short functional operations.

Detailed Explanation

An arrow function is a compact function syntax introduced in ES6. It is especially convenient for callbacks and short functional operations.

Arrow functions differ from ordinary functions in important ways: they do not create their own this, arguments, super, or new.target bindings, and they cannot be used as constructors with new. Their lexical this behavior makes them useful inside callbacks where the surrounding this value should be preserved.

Code Example

const square = n => n * n;
console.log(square(5)); // 25

const add = (a, b) => {
    return a + b;
};

console.log(add(2, 4)); // 6