π INTRODUCTION
The spread operator (...) is a powerful feature introduced in ES6 that allows you to expand or βspreadβ elements of arrays or objects.
It is widely used in:
- React applications
- Array manipulation
- Data merging
π In this program, we will learn how to merge arrays using the spread operator.
π PROBLEM STATEMENT
π Write a program to merge arrays using the spread syntax.
π‘ CONCEPTS USED
- Spread Operator (
...) - Arrays in JavaScript
- ES6 Features
π§ LOGIC EXPLANATION
- Create two arrays
- Use spread operator
... - Combine arrays into a new array
- Display the result
π» PROGRAM CODE (Using Spread Operator)
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
// Merging arrays using spread operator
const mergedArray = [...arr1, ...arr2];
console.log("Array 1:", arr1);
console.log("Array 2:", arr2);
console.log("Merged Array:", mergedArray);
π₯οΈ OUTPUT
Array 1: [1, 2, 3]
Array 2: [4, 5, 6]
Merged Array: [1, 2, 3, 4, 5, 6]
π STEP-BY-STEP EXPLANATION
...arr1β spreads elements of first array...arr2β spreads elements of second array- Combined into a new array
β‘ OLD METHOD (WITHOUT SPREAD)
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const merged = arr1.concat(arr2);
console.log(merged);
π― ADVANCED EXAMPLES
1. Adding Elements While Merging
const merged = [...arr1, 100, ...arr2];
console.log(merged);
2. Copying an Array
const copy = [...arr1];
console.log(copy);
3. Merging Objects
const obj1 = { name: "Nyra" };
const obj2 = { course: "BBA" };
const mergedObj = { ...obj1, ...obj2 };
console.log(mergedObj);
π ADVANTAGES OF SPREAD OPERATOR
- Short and clean syntax
- Easy array merging
- Avoids modifying original arrays
- Widely used in React state updates
π REAL-WORLD USE CASE
- Combining API data
- Updating React state
- Copying arrays/objects safely
β VIVA QUESTIONS (IMPORTANT)
Q1. What is the spread operator?
π It is ... used to expand elements of arrays or objects.
Q2. Which ES version introduced spread operator?
π ES6 (ECMAScript 2015)
Q3. Can spread operator be used with objects?
π Yes, for merging and copying objects.
Q4. Does spread operator modify original array?
π No, it creates a new array.
Q5. What is the difference between concat() and spread?
π Spread is shorter and more flexible.
π RELATED POSTS
π MCA 168 Full Stack Development Lab Programs List
π Arrow Function in JavaScript β Program 1
π Template Literals in JavaScript β Program 2
π Destructuring Assignment β Program 3
π CONCLUSION
The spread operator is one of the most useful ES6 features. It simplifies array and object operations and is heavily used in modern JavaScript development.
π Practice this concept β it is frequently asked in exams and interviews.
Further Reading
How to Master Full Stack Development?
Spring Framework Practice Problems and Their Solutions
From Google to the World: The Story of Go Programming Language
Why Go? Understanding the Advantages of this Emerging Language
Creating and Executing Simple Programs in Go
20+ Interview Questions on Go Programming Language
100+ MCQs On Java Architecture
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
