
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Rest and Spread operators in JavaScript
The rest operator (…) allows us to call a function with any number of arguments and then access those excess arguments as an array. The rest operator also allows us in destructuring array or objects.
The spread operator (…) allows us to expand an iterable like array into its individual elements.
Example
Following is the code showing the rest and spread operator in JavaScript −
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample, .result { font-size: 20px; font-weight: 500; } </style> </head> <body> <h1>JavaScript Rest and spread operator</h1> <div class="sample"></div> <div style="color: green;" class="result"></div&g; <button class="btn">Spread Operator</button> <h3> Click on the above button to concatenate array using spread operator </h3> <button class="btn">Rest Operator</button> <div style="color: green;" class="result"></div> <h3> Click on the above button to add some numbers using rest operator </h3> <script> let sampleEle = document.querySelector(".sample"); let btnEle = document.querySelectorAll(".btn"); let resEle = document.querySelectorAll(".result"); let arr = [2, 3, 4, 5]; let arr1 = ["a", "b", "c", "d"]; sampleEle.innerHTML = "arr = " + arr + "<br> arr1 = " + arr1; function addArr(num, ...ar) { let sum = num; ar.forEach((item) => { sum += item; }); resEle[1].innerHTML = "Sum of the elements = " + sum; } btnEle[0].addEventListener("click", () => { resEle[0].innerHTML = "Concatenated array = " + [...arr, ...arr1]; }); btnEle[1].addEventListener("click", () => { addArr(44, 11, 35, 44, 22, 99); }); </script> </body> </html>
Output
The above code will produce the following output −
On clicking the “Spread Operator” button −
On clicking the “Rest Operator” button −
- Related Articles
- Usage of rest parameter and spread operator in JavaScript?
- JavaScript Spread Operator
- What is spread Operator (...) in JavaScript?
- Spread operator for arrays in JavaScript
- Spread operator in function calls JavaScript
- Explain the benefits of spread syntax & how it is different from rest syntax in ES6?
- Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?
- What are operators in JavaScript?
- What are Rest parameters in JavaScript?
- Effective Function Signatures with Default and Rest Parameters in JavaScript
- What are JavaScript Operators
- What are Arithmetic Operators in JavaScript?
- What are Comparison Operators in JavaScript?
- What are Logical Operators in JavaScript?
- What are Assignment Operators in JavaScript?

Advertisements