In this chapter, we will learn about JavaScript arrow functions. Arrow functions provide a shorter and more concise syntax for writing functions. We will cover:
- What are Arrow Functions?
- Syntax
- Arrow Functions with No Parameters
- Arrow Functions with One Parameter
- Arrow Functions with Multiple Parameters
- Implicit Return
- Lexical
this
Binding - Simple Programs using Arrow Functions
What are Arrow Functions?
Arrow functions, introduced in ES6 (ECMAScript 2015), provide a shorter syntax for writing functions. They are always anonymous and are best suited for non-method functions. Arrow functions do not have their own this
, arguments
, super
, or new.target
bindings.
Syntax
The syntax for an arrow function is as follows:
let functionName = (parameters) => { // code to be executed };
Arrow Functions with No Parameters
If the function does not take any parameters, you can write it as follows:
Example
let greet = () => { console.log("Hello, World!"); }; greet(); // Output: Hello, World!
Arrow Functions with One Parameter
If the function takes exactly one parameter, you can omit the parentheses around the parameter.
Example
let square = x => { return x * x; }; console.log(square(4)); // Output: 16
Arrow Functions with Multiple Parameters
If the function takes multiple parameters, you need to include the parentheses around the parameters.
Example
let add = (a, b) => { return a + b; }; console.log(add(2, 3)); // Output: 5
Implicit Return
If the function body consists of a single expression, you can omit the curly braces and the return
keyword. The expression will be implicitly returned.
Example
let multiply = (a, b) => a * b; console.log(multiply(3, 4)); // Output: 12
Lexical this
Binding
Arrow functions do not have their own this
context. Instead, they inherit this
from the surrounding scope. This is particularly useful when working with callbacks.
Example
function Person(name) { this.name = name; // Using a regular function this.sayNameRegular = function() { setTimeout(function() { console.log("Regular function:", this.name); // `this` is undefined }, 1000); }; // Using an arrow function this.sayNameArrow = function() { setTimeout(() => { console.log("Arrow function:", this.name); // `this` refers to the Person object }, 1000); }; } let person = new Person("Amit"); person.sayNameRegular(); // Output: Regular function: undefined person.sayNameArrow(); // Output: Arrow function: Amit
Simple Programs using Arrow Functions
Program 1: Calculate the Square of a Number
let square = x => x * x; console.log("Square of 5:", square(5)); // Output: Square of 5: 25
Program 2: Filter Even Numbers from an Array
let filterEvenNumbers = numbers => numbers.filter(number => number % 2 === 0); let numbers = [1, 2, 3, 4, 5, 6]; console.log("Even numbers:", filterEvenNumbers(numbers)); // Output: Even numbers: [2, 4, 6]
Program 3: Convert Celsius to Fahrenheit
let celsiusToFahrenheit = celsius => (celsius * 9/5) + 32; console.log("25°C is", celsiusToFahrenheit(25), "°F"); // Output: 25°C is 77°F
Program 4: Generate a Random Number between a Range
let getRandomNumber = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min; console.log("Random number between 1 and 100:", getRandomNumber(1, 100));
Program 5: Find the Maximum Number in an Array
let findMax = numbers => Math.max(...numbers); let numbers = [10, 20, 30, 5, 15]; console.log("Maximum number:", findMax(numbers)); // Output: Maximum number: 30
Conclusion
In this chapter, you learned about JavaScript arrow functions, including their syntax, how to use them with no parameters, one parameter, and multiple parameters, implicit return, and lexical this
binding. We also provided various use cases with simple programs to demonstrate the usage of arrow functions. Arrow functions offer a more concise and readable way to write functions, especially for short and simple operations.