Monday, 4 Aug 2025
  • My Interests
  • My Saves
  • Try Intents
Subscribe
Focus - Code Reveals
  • Home
  • HTML
    What is Symentic HTML

    What is Symentic HTML?

    By Chief Editor

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

    By Chief Editor

    What are the different types of HTML tags?

    By Chief Editor

    What is the difference between “HTML” and “HTML5”?

    By Chief Editor

    What is Doctype HTML in HTML?

    By Chief Editor

    What is a Meta Tag in HTML?

    By Chief Editor
  • JavaScript

    How to Reverse a String in JavaScript: Two Essential Methods

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor
    What are the Lexical Scope in JavaScript

    What are the Lexical Scope in JavaScript?

    By Chief Editor

    What is Scope in JavaScript?

    By Chief Editor

    Explain var let and const in JavaScript with Example.

    By Chief Editor

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

    By Chief Editor
  • Frontend Interview

    Difference Between position: relative and position: absolute in CSS

    By Chief Editor

    Is JavaScript a synchronous or asynchronous language?

    By Chief Editor

    What is Position in CSS?

    By Chief Editor

    Explain Deep Copy and Shallow Copy in JavaScript.

    By Chief Editor

    What is Symentic HTML?

    By Chief Editor

    What are the Lexical Scope in JavaScript?

    By Chief Editor
  • Backend Interview

    What is NodeJS and its main features?

    By Chief Editor

    What is a Meta Tag in HTML?

    By Chief Editor

    What is Redux, and how does it work?

    By Chief Editor

    What is one-way data binding in React?

    By Chief Editor

    What is Synthetic Event?

    By Chief Editor

    Mastering Star Rating Systems: Best Practices and Optimized Code Examples

    By Chief Editor
  • Nodejs
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate
  • 🔥
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • React Interview
  • HTML
  • Frontend Interview
  • CSS
  • Redux
  • Javascript
  • System Design
Font ResizerAa
Focus - Code RevealsFocus - Code Reveals
  • My Saves
  • My Interests
  • My Feed
  • History
  • Technology
Search
  • Homepage
  • ReactJS
  • JavaScript
  • JavaScript Interview
  • HTML
  • CSS
  • Backend Interview
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?

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

How to Improve the Performance of React Applications

React is a powerful library for building interactive user interfaces, but performance issues can arise…

By Chief Editor

What is Flex Box in CSS?

Flexbox, short for Flexible Box, is a CSS layout model designed to make it easier…

By Chief Editor

What are controlled and uncontrolled components in React?

When working with form inputs in React, you will often hear the terms Controlled Components…

By Chief Editor

You Might Also Like

JavaScript

Explain Call, Apply and Bind in JavaScript.

By Chief Editor
JavaScript

Explain var let and const in JavaScript with Example.

By Chief Editor
Frontend InterviewJavaScript

How to Reverse a String in JavaScript: Two Essential Methods

By Chief Editor
Frontend InterviewJavaScriptJavaScript Interview

Is JavaScript a synchronous or asynchronous language?

By Chief Editor

Focus by CodeReveals is your dedicated platform for mastering tech interviews and advancing your development skills. Whether you’re aiming to become a Frontend Developer, Backend Developer, or simply preparing for your next big interview, we’re here to guide you every step of the way.

Our mission is to help aspiring developers gain real-world knowledge, build confidence, and succeed in technical interviews. We offer structured content, curated interview questions, coding challenges, and practical guidance — all designed by experienced professionals who understand what top tech companies are looking for.

At Improve, we don’t just teach — we prepare you to think like a developer, solve like an engineer, and present like a pro.

Join us and start improving today — because your dream tech job is within reach

Most Famous
  • HTML
  • CSS
  • JavaScript
  • NodeJS
Top Categories
  • JavaScript Interview
  • React Interview
  • Frontend Interview
  • Backend Interview
Usefull Links
  • Contact Us
  • Advertise with Us
  • Complaint
  • Cookies Policy
  • Privacy Policy
  • Donate

©2025  Code Reveals Inc. All Rights Reserved.

Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?