Friday, 18 Jul 2025
  • My Interests
  • My Saves
  • Try Intents
Subscribe
improve-logo improve-logo
  • Home
  • HTML

    What is a Meta Tag in HTML?

    By Chief Editor
    What is Symentic HTML

    What is Symentic HTML?

    By Chief Editor

    What are the different types of HTML tags?

    By Chief Editor

    What is Doctype HTML in HTML?

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor

    Difference between HTML Tag and HTML Element in HTML?

    By Chief Editor
  • JavaScript

    Difference between document.createElement and document.createElementFragement in JavaScript?

    By Chief Editor

    What are the map, filter, and reduce methods in JavaScript?

    By Chief Editor

    Explain var let and const in JavaScript with Example.

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    What is a Promise in JavaScript, and what are its parameters?

    By Chief Editor

    What is the this Keyword in JavaScript?

    By Chief Editor
  • Frontend Interview

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    What is Block level Element and Inline Level Element?

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor
  • Backend Interview

    What is Higher Order Component in React?

    By Chief Editor

    What is React Fiber and its importance in react?

    By Chief Editor

    How can you delay the dispatch in React?

    By Chief Editor

    What is Virtual DOM in React?

    By Chief Editor

    What are the async and defer attributes in the “script” tag?

    By Chief Editor

    What is state in React, and what are its properties?

    By Chief Editor
  • Nodejs
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Donate
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
ImproveImprove
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • Pages
    • Home
    • Blog Index
    • Contact Us
    • Search Page
    • 404 Page
  • Features
    • Post Headers
    • Layout
  • Personalized
    • My Feed
    • My Saves
    • My Interests
    • History
  • About
  • Categories
    • Technology
  • Categories
Have an existing account? Sign In
Follow US
© 2022 Code Reveals Inc. All Rights Reserved.

Home What are the Rest and Spread operators in JavaScript?

JavaScriptJavaScript Interview

What are the Rest and Spread operators in JavaScript?

Chief Editor
Last updated: February 16, 2025 5:58 pm
Chief Editor
Share
SHARE

Rest and Spread Operators in JavaScript (...)

The Rest Operator and Spread Operator both use three dots (...), but they serve different purposes depending on how and where they are used.

Contents
🟢 1. Rest Operator (...)🟣 2. Spread Operator (...)Rest vs Spread Summary:Example Using Both Rest and Spread Together:When to Use:

🟢 1. Rest Operator (...)

The Rest Operator is used in function parameters to collect multiple arguments into a single array.

Syntax:

javascriptCopyEditfunction myFunction(...args) {
  console.log(args);
}

Key Points:

  • Gathers remaining arguments into an array.
  • Used in function parameters.
  • Always comes at the end of the function parameters.

Example:

javascriptCopyEditfunction sum(...numbers) {
  return numbers.reduce((acc, curr) => acc + curr, 0);
}

console.log(sum(1, 2, 3, 4)); // Output: 10

Another Example with Mixed Parameters:

javascriptCopyEditfunction greet(greeting, ...names) {
  console.log(`${greeting}, ${names.join(', ')}`);
}

greet('Hello', 'John', 'Jane', 'Doe');
// Output: Hello, John, Jane, Doe

🟣 2. Spread Operator (...)

The Spread Operator is used to expand elements of an iterable (like an array or object) into individual elements.

Key Points:

  • Expands elements of an array, object, or string.
  • Used in array literals, function calls, and object literals.

Spread with Arrays:

javascriptCopyEditconst numbers = [1, 2, 3];
const newNumbers = [...numbers, 4, 5];
console.log(newNumbers); // [1, 2, 3, 4, 5]

Spread with Objects:

javascriptCopyEditconst user = { name: 'John', age: 25 };
const updatedUser = { ...user, city: 'New York' };
console.log(updatedUser);
// Output: { name: 'John', age: 25, city: 'New York' }

Spread in Function Calls:

javascriptCopyEditconst numbers = [1, 2, 3];
console.log(Math.max(...numbers)); // Output: 3

Rest vs Spread Summary:

FeatureRest Operator (...args)Spread Operator (...array)
PurposeCollects multiple values into an arraySpreads array/object elements out
UsageFunction ParametersArrays, Objects, Function Calls
ResultCreates an arrayExpands values into individual elements
Examplefunction sum(...nums) {}[...array1, ...array2]

Example Using Both Rest and Spread Together:

javascriptCopyEditfunction multiply(multiplier, ...numbers) {
  return numbers.map(num => num * multiplier);
}

const nums = [1, 2, 3];
console.log(multiply(2, ...nums)); // [2, 4, 6]

When to Use:

Use CaseOperator
Combine ArraysSpread (...)
Clone Arrays/ObjectsSpread (...)
Pass Array as ArgumentsSpread (...)
Collect Function ArgumentsRest (...)
Handle Variable ParametersRest (...)

The Rest and Spread Operators make JavaScript code more concise and readable, especially in modern ES6+ development.

Share This Article
Email Copy Link Print
Previous Article What is memoization in JavaScript?
Next Article What is one-way data binding in React?
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Your Trusted Source for Accurate and Timely Updates!

Our commitment to accuracy, impartiality, and delivering breaking news as it happens has earned us the trust of a vast audience. Stay ahead with real-time updates on the latest events, trends.
FacebookLike
XFollow
InstagramFollow
YoutubeSubscribe
LinkedInFollow
QuoraFollow
- Advertisement -
Ad imageAd image

Popular Posts

What are Benefits in React?

What are the Benefits of Using React? React is a popular JavaScript library for building…

By Chief Editor

System Design and Frontend System Design: An In-depth Overview

Introduction System design is an important component of software engineering that entails the system architecture,…

By Chief Editor

What is React Fiber and its importance in react?

React Fiber is the reimplementation of React's core reconciliation algorithm, introduced in React 16 (2017).It…

By Chief Editor

You Might Also Like

JavaScriptJavaScript Interview

What is the this Keyword in JavaScript?

By Chief Editor
JavaScriptJavaScript Interview

Difference between document.createElement and document.createElementFragement in JavaScript?

By Chief Editor
JavaScript

Explain Call, Apply and Bind in JavaScript.

By Chief Editor
JavaScriptJavaScript Interview

What is one-way data binding in React?

By Chief Editor
improve-logo

Code Reveals is a cutting-edge software development company dedicated to delivering high-quality, scalable, and innovative solutions for businesses of all sizes. Our team of expert developers, designers, and engineers specializes in creating custom software, web applications, mobile apps, and enterprise solutions that are tailored to meet the unique needs of our clients.

Most Famous
  • HTML
  • CSS
  • JavaScript
  • Node
Top Categories
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
Usefull Links
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Donate

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?