Lecture

Spread Operator

The Spread Operator consists of three dots (...).

  • The Spread Operator is used to 'spread' the elements of an array or object.

  • It is mainly used to combine or copy arrays and objects.


Examples of Using the Spread Operator

Arrays

Combining Arrays
const arr1 = [1, 2, 3]; const arr2 = [4, 5, 6]; const combined = [...arr1, ...arr2]; // [1, 2, 3, 4, 5, 6] console.log(combined);
Copying Arrays
const original = [1, 2, 3]; const copied = [...original]; console.log(copied); // [1, 2, 3]

Objects

Combining Objects
const obj1 = { a: 1, b: 2 }; const obj2 = { b: 3, c: 4 }; const combined = { ...obj1, ...obj2 }; console.log(combined); // { a: 1, b: 3, c: 4 }
Copying Objects
const obj1 = { a: 1, b: 2 }; const copiedObj = { ...obj1 }; // {a: 1, b: 2} console.log(copiedObj)
Mission
0 / 1

The spread operator is used to merge arrays and objects.

True
False

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

Code Editor

Run

Execution Result