
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use spread operator to join two or more arrays in JavaScript?
Two join two or more arrays we have a built-in method called array.concat(). But we can join arrays much more easily by using spread operator.
Syntax
var merged = [...arr1, ...arr2];
Lets' try to merge arrays without spread operator.
In the following example, instead of the spread operator, array.concat() method is used to join two arrays.
Example
<html> <body> <script> var arr1 = [1,2,3]; var arr2 = [4,5,6]; var merged = arr1.concat(arr2); document.write(merged); </script> </body> </html>
Output
1,2,3,4,5,6
Spread operator
In the following example, spread operator is used to join two arrays.
Example
<html> <body> <script> var arr1 = [1,2,3]; var arr2 = [4,5,6]; var merged = [...arr1, ...arr2]; document.write(merged); </script> </body> </html>
Output
1,2,3,4,5,6
In the following example, spread operator is used to join 3 arrays. By using concat() method it is difficult if there are more arrays but by using spread operator it is very easy to join more number arrays.
Example
<html> <body> <script> var arr1 = [1,2,3]; var arr2 = [4,5,6]; var arr3 = [7,8,9]; var merged = [...arr1,...arr2,...arr3]; document.write(merged); </script> </body> </html>
Output
1,2,3,4,5,6,7,8,9
- Related Questions & Answers
- Spread operator for arrays in JavaScript
- How to join two arrays in JavaScript?
- JavaScript Spread Operator
- How to find the common elements between two or more arrays in JavaScript?
- What is spread Operator (...) in JavaScript?
- Spread operator in function calls JavaScript
- How to clone an array using spread operator in JavaScript?
- How to clone an object using spread operator in JavaScript?
- Join arrays to form string in JavaScript
- How to use Spread Syntax with arguments in JavaScript functions?
- How to join or concatenate two lists in C#?
- How to add two or more strings in MySQL?
- How to concatenate two or more vectors in R?
- How to find maximum value in an array using spread operator in JavaScript?
- Using JSON.stringify() to display spread operator result?
Advertisements