Tuesday, 3 Jun 2025
  • My Feed
  • My Interests
  • My Saves
  • History
  • Blog
Subscribe
Code Reveals
  • Home
  • HTML

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

    By admin

    What is Doctype HTML in HTML?

    By admin
    What is Symentic HTML

    What is Symentic HTML?

    By admin

    What is a Meta Tag in HTML?

    By admin

    What are the different types of HTML tags?

    By admin

    What is Block level Element and Inline Level Element?

    By admin
  • JavaScript

    What is the this Keyword in JavaScript?

    By admin

    Explain Call, Apply and Bind in JavaScript.

    By admin

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

    By admin

    What is memoization in JavaScript?

    By admin

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

    By admin

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

    By admin
  • Frontend Interview

    What are the Lexical Scope in JavaScript?

    By admin

    What is Block level Element and Inline Level Element?

    By admin

    Is JavaScript a synchronous or asynchronous language?

    By admin

    Difference Between position: relative and position: absolute in CSS

    By admin

    What is Position in CSS?

    By admin

    What is Symentic HTML?

    By admin
  • Backend Interview

    What is localization in React?

    By admin

    What is the Event Loop in JavaScript?

    By admin

    What is memoization in JavaScript?

    By admin

    What is Block level Element and Inline Level Element?

    By admin

    How can you delay the dispatch in React?

    By admin

    Lazy Loading in React.js: Boosting Performance and Reducing Load Time

    By admin
  • Other
    • Contact Us
  • Frontend Interview
  • Backend Interview
  • React Interview
  • JavaScript Interview
  • Contacts Us
  • Advertise with Us
  • Complaint
  • Privacy Policy
  • Cookie Policy
  • Submit a Tip
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
Code RevealsCode Reveals
  • 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 Blog What are the Rest and Spread operators in JavaScript?
JavaScriptJavaScript Interview

What are the Rest and Spread operators in JavaScript?

admin
Last updated: February 16, 2025 5:58 pm
admin
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 is Symentic HTML?

Semantic HTML refers to using HTML tags that convey the meaning or purpose of the…

By admin

What are the different types of HTML tags?

HTML tags are the building blocks of an HTML document. They define the structure and…

By admin

What is Redux, and how does it work?

Redux is a state management library for JavaScript applications, often used with libraries like React.…

By admin

You Might Also Like

JavaScriptJavaScript Interview

What is the this Keyword in JavaScript?

By admin
What are the Lexical Scope in JavaScript
Frontend InterviewJavaScript

What are the Lexical Scope in JavaScript?

By admin
JavaScript

What is hoisting in JavaScript with an example?

By admin
JavaScriptJavaScript Interview

What is one-way data binding in React?

By admin

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
  • Submit a Tip

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?