Spread Operator in JavaScript3 Mar 2025 | 4 min read The spread operator is used to expand elements of an iterable such as a string, array or object. We will learn about the spread operator in this article. Let us see an example without using the spread operator: We will create an array called 'fruits' and print the array without using the spread operator. Code: Output: mango apple pear In the above example, we have printed all items of the array 'fruits' by using their index number. Now, let us look at an example of using a spread operator: We will create an array called 'fruits' and print the array using the spread operator. Code: Output: mango apple pear In this example, we have not used index numbers to print all items instead we have used spread operators. We can see that the code looks cleaner and the line code is reduced. Benefits of the spread operatorWe can use the spread operator for many things which are as follows:
Following are some of the demonstrations of using the spread operator in JavaScript: Demo 1: Expanding the ArrayWe will expand the elements of an array in this demo. Code: Output: [ 'red', 'blue', 'yellow' ] red blue yellow Demo 2: Spread Operator inside ArrayWe will expand the elements of an array by utilizing the spread operator inside an array. Code: Output: We have not used the spread operator to print the colors1 array so we can see that it contains an inner array. We have used the spread operator to print the colors2 array so we can see elements of colors array becomes the individual string elements. [ 'orange', [ 'red', 'blue', 'yellow' ], 'brown' ] [ 'orange', 'red', 'blue', 'yellow', 'brown' ] Demo 3: Duplicating Object LiteralsWe will utilize the spread operator to duplicate object literals in this demo. Code: Output: We can see the duplicate object literals in the output. { cars_company: { company1: 'BMW', company2: 'Tesla', company3: 'Ferrari' }} { company1: 'BMW', company2: 'Tesla', company3: 'Ferrari' } Demo 4: Merging Object LiteralsWe will utilize the spread operator to merge object literals in this demo. Code: Output: We have merged student1and student2 to team1 using the spread operator and we can see that both objects are flawlessly merged to form a new object. We have merged student1and student2 to team2 without using the spread operator and we can see both objects name also. team1 = { name: 'Katy', age: '36' } team2 = { student1: { name: 'Ben', age: '25' }, student2: { name: 'Katy', age: '36' } } Demo 5: Spread Operator as Part of Function ArgumentWe can use the spread operator as part of the function argument. We will create a function that takes three arguments. Code: Output: We can witness that only the first three arguments are considered to print the sum of numbers. 18 Demo 6: Spread Operator with StringsWe can use the spread operator with strings in this demo. Code: Output: [ 'zathura' ] [ 'z', 'a', 't', 'h', 'u', 'r', 'a' ] Conclusion:In this article, we have studied the spread operator in JavaScript. We have seen various ways to utilize the spread operator with the help of demonstrations. Next TopicJavaScript Problem Solving |
In the world of web development, JavaScript Object Notation (JSON) has become the de facto standard for data interchange. JSON provides a lightweight and readable format for storing and exchanging data between a server and a web application. When working with JSON data in JavaScript, it's...
6 min read
What is ? In JavaScript, Astro.js is a web framework which is optimized for building fast and content driven websites. It is a modern JS based framework that help to build fast content websites. In simple words, refers to JavaScript code which is used within the context...
6 min read
An element of document structure is stored using the DocumentFragment interface, a simplified form of the Document interface. However, the current DOM tree does not include a DocumentFragment. If you edit the document fragment, the document does not affect the speed costs. The appendChild() or insertBefore()...
4 min read
Overview In Node.js, event emitters are objects that signal the completion of an action by sending a message. JavaScript programmers can write code that listens for events from an event emitter and then triggers functions each time the event is triggered. An identifying string and any data...
5 min read
What is Framework in JavaScript? In JavaScript, frameworks are pre-written code JavaScript libraries that can provide developers with pre-built solutions for the common tasks and challenges in web development. Most of the frameworks in JavaScript often include functions, modules, and APIs that help us simplify the process...
6 min read
In JavaScript, controlling strings is a key operation. One normal errand is to check if a string contains a particular substring. Substrings are coterminous successions of characters inside a string. Whether you're working with web improvement, data handling, or some other JavaScript application, knowing how...
3 min read
What is Debugging in JavaScript? In JavaScript, debugging is used to find errors or bugs in the source code of any software. In simple words, debugging is a process in which we examine the JavaScript code find the errors, and fix them. Developers can make some mistakes...
4 min read
Javascript regular expression uses the "$" quantifier to get an input character at the end of the string. The $ quantifier filters the value at the end of input information using methods. The modifier uses the regexp method and quantifier to operate, replace, search, and...
13 min read
The date is an essential function for user interaction form, table and database. We can work on the date in the format of the user's requirement. The users mostly use the following date format to display the date. dd/mm/yyyy format mm/dd/yyyy format yyyy/dd/mm format dd/month_name /yyyy format Methods to Convert...
5 min read
Clear Intervals are fundamental additives of JavaScript that might be used to manage movements that want to be finished often or after a put-off and to manipulate the drift of execution. These capabilities are provided through the setInterval and setTimeout features, which let programmers write code...
9 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India