Arrow functions, also known as “fat arrow” functions, are a shorter syntax for writing function expressions in JavaScript. They are defined using the “=>” operator and do not have their own “this” binding, instead they inherit the “this” value of the surrounding scope.
Here is an example of an arrow function that takes in a single argument “x” and returns its square:
let square = x => x * x;
console.log(square(5)); // 25
Arrow functions can also have multiple arguments, like so:
let add = (a, b) => a + b;
console.log(add(1, 2)); // 3
Arrow functions are also useful when working with arrays, as they can be used in method calls such as ‘map’, ‘filter’, and ‘reduce’.
let numbers = [1, 2, 3, 4, 5];
let squaredNumbers = numbers.map(x => x * x);
console.log(squaredNumbers); // [1, 4, 9, 16, 25]
Also Read:
- What Is a Web Worker In JavaScript?
- How To Sort an Array of Strings In JavaScript?
- ECMAScript vs JavaScript
- What Is Strict Mode In JavaScript?
- Difference Between Node.js and AngularJS With Example
- Difference Between == And === In JavaScript
- What Is JSON In JavaScript
- How To Go Back To Previous Page In JavaScript?
- How To Detect A Mobile Device With JavaScript?
- How To Close The Current Tab In A Browser Window Using JavaScript?
- How To Convert Input Text To Uppercase While Typing Using JavaScript?
- How To Show A Confirmation Message Before Delete In JavaScript?
- How To Detect Browser or Tab Closing In JavaScript?
- How To Get Hash Value From URL Using JavaScript?
- How To Get The Name, Size, And Type Of A File In JavaScript?
- Run JavaScript From The Command Line