Skip to main content
FREE Mock interview & resume review with Sumit. Free right now, no payment needed.Book a slot

Spreading

Topic 3JavaScript Part 5 (Important Keywords)

Spreading

Introduction

The spread operator (`...`) expands an array, object, or iterable into its individual elements. It is useful for copying, merging, or destructuring.

Key Concepts and Definitions

Spread Operator

The `...` operator expands an array, object, or iterable into its individual elements. Useful for copying, merging, or destructuring.

Examples

Array Spreading:

const nums1 = [1, 2, 3];
const nums2 = [4, 5, 6];
const combined = [...nums1, ...nums2];

console.log(combined); // [1, 2, 3, 4, 5, 6]

Object Spreading:

const obj1 = { name: 'Alice', age: 25 };
const obj2 = { city: 'New York' };
const merged = { ...obj1, ...obj2 };

console.log(merged); // { name: 'Alice', age: 25, city: 'New York' }

Swapping Variables:

let a = 1, b = 2;
[a, b] = [b, a];
console.log(a, b); // 2, 1

Interview guide discussion

(0)

Join the discussion — login to comment and help other developers.

Login to comment