JavaScript Spread Operator
Last Updated :
05 Jun, 2025
The Spread operator (represented as three dots or …) is used on iterables like array and string, or properties of Objects, to expand wherever zero or more elements are required to be copied or assigned.
1. Adding Multiple Elements Using Spread Operator
Even though we get the content on one array inside the other one, actually it is an array inside another array which is definitely what we didn't want. If we want the content to be inside a single array we can make use of the spread operator.
javascript
// expand using spread operator
let a = [10, 20];
let b = [...a, 30, 40];
console.log(a);
We can insert at the beginning and both begin and end together also
JavaScript
// expand using spread operator
let a = [10, 20];
let b = [30, 40, ...a, 50, 60];
console.log(a);
2. Find Min / Max using Spread Operator
Math object method won’t work and will return NaN. When …arr is used in the function call, it “expands” an iterable object arr into the list of arguments In order to avoid this NaN output, we make use of a spread operator. we make use of a spread operator In order to avoid this NaN
javascript
// Min in an array using Math.min()
let a = [1,2,3,-1];
console.log(Math.min(a)); //NaN
// Now using spread
console.log(Math.min(...a));
3. Passing Array Elements as Function Parameters
JavaScript
function add(x, y, z) {
return x + y + z;
}
let a = [10, 20, 30];
console.log(add(...a));
4. Copying Array using Spread
We are copying all the elements of the given array to the another new array by the use of the spread operator.
JavaScript
const a = [1, 2, 3];
const b = [...a];
console.log(b);
// Please note that in JavaScript, doing
// b = a does not create a clone. It only creates
// one more reference. You may try uncommening the
// below code
// const c = [1, 2, 3];
// const d = c;
// d.push(4);
// console.log(c); // Prints [1, 2, 3, 4]
Please refer Clone an array for different methods of copying an array in JS
5. Concatenate Arrays using Spread Operator
The spread operator can be used to concatenate more than one array.
javascript
// Spread operator for array concatenation
let a = [1, 2, 3];
let b = [4, 5];
a = [...a, ...b];
console.log(a);
Note: Though we can achieve the same result as the concat method, it is not recommended to use the spread in this particular case, as for a large data set it will work slower when compared to the native concat() method.
6. Working of Objects with Spread Operator
ES6 has added spread property to object literals in javascript. The spread operator (...) with objects is used to create copies of existing objects with new or updated values or to make a copy of an object with more properties. Let's take an example of how to use the spread operator on an object,
javascript
const usr = {
name: 'Jen',
age: 22
};
const cloneUsr = { ...usr };
console.log(cloneUsr);
Output{ name: 'Jen', age: 22 }
Here we are spreading the usr object. All key-value pairs of the usr object are copied into the cloneUsr object.
Let’s look at another example of merging two objects using the spread operator.
javascript
const usr1 = {
name: 'Jen',
age: 22,
};
const usr2 = {
name: "Andrew",
location: "Philadelphia"
};
const mergedUsers = { ...usr1, ...usr2 };
console.log(mergedUsers);
Output{ name: 'Andrew', age: 22, location: 'Philadelphia' }
The mergedUsers is a copy of usr1 and usr2. Actually, every enumerable property on the objects will be copied to the mergedUsers object. The spread operator is just a shorthand for the Object.assign() method but, there are some differences between the two.
Below is an example of adding properties to an object using spread operator.
JavaScript
const o1 = { a: 1, b: 2 };
const o2 = { ...o1, b: 3, c: 4 };
console.log(o2);
Output{ a: 1, b: 3, c: 4 }
We have a complete list of Javascript Operators, to check those please go through the Javascript Operators Complete Reference article.
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
Top 10 Projects For Beginners To Practice HTML and CSS Skills Learning to code is an exciting journey, especially when stepping into the world of programming with HTML and CSSâthe foundation of every website you see today. For most beginners, these two building blocks are the perfect starting point to explore the creative side of web development, designing vis
8 min read
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel
7 min read