Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 2 of 10 · ES6+ Features
Interview question

How do the spread and rest operators work, and how are they different? Spread और rest operators कैसे काम करते हैं, और कैसे अलग हैं?

Answer

Both use the same ... syntax, but spread expands an iterable into individual elements, while rest collects multiple elements into a single array or object.

OperatorPurposeContext
SpreadExpands array/object into individual itemsFunction calls, array/object literals
RestCollects remaining items into an array/objectFunction parameters, destructuring
// SPREAD - arrays
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const combined = [...arr1, ...arr2];
console.log(combined); // [1, 2, 3, 4, 5, 6]

const copy = [...arr1]; // shallow copy

// SPREAD - objects
const obj1 = { a: 1, b: 2 };
const obj2 = { c: 3 };
const merged = { ...obj1, ...obj2 };
console.log(merged); // { a: 1, b: 2, c: 3 }

// SPREAD in function calls
function sum(a, b, c) { return a + b + c; }
const nums = [1, 2, 3];
console.log(sum(...nums)); // 6 - expands array into arguments

// SPREAD to copy with overrides (immutable updates)
const user = { name: 'John', age: 30 };
const updatedUser = { ...user, age: 31 }; // creates new object, doesn't mutate original
console.log(updatedUser); // { name: 'John', age: 31 }

// REST - function parameters, collects remaining args
function multiply(multiplier, ...numbers) {
    return numbers.map(n => n * multiplier);
}
console.log(multiply(2, 1, 2, 3)); // [2, 4, 6]

// REST in destructuring - collects remaining elements
const [firstItem, ...restItems] = [1, 2, 3, 4];
console.log(firstItem, restItems); // 1 [2, 3, 4]

// REST in object destructuring
const { id, ...otherProps } = { id: 1, name: 'John', age: 30 };
console.log(id, otherProps); // 1 { name: 'John', age: 30 }

दोनों same ... syntax use करते हैं, पर spread iterable को individual elements में expand करता है, जबकि rest multiple elements को एक array/object में collect करता है।

Operatorउद्देश्यContext
SpreadArray/object को individual items में expand करता हैFunction calls, literals
Restबचे हुए items को array/object में collect करता हैFunction parameters, destructuring
// SPREAD - arrays
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const combined = [...arr1, ...arr2];
console.log(combined); // [1, 2, 3, 4, 5, 6]

// SPREAD - objects
const obj1 = { a: 1, b: 2 };
const merged = { ...obj1, c: 3 };

// Function calls में
function sum(a, b, c) { return a + b + c; }
const nums = [1, 2, 3];
console.log(sum(...nums)); // 6

// Immutable update
const user = { name: 'John', age: 30 };
const updatedUser = { ...user, age: 31 };

// REST - function parameters
function multiply(multiplier, ...numbers) {
    return numbers.map(n => n * multiplier);
}
console.log(multiply(2, 1, 2, 3)); // [2, 4, 6]

// REST destructuring
const [firstItem, ...restItems] = [1, 2, 3, 4];
console.log(firstItem, restItems); // 1 [2, 3, 4]

const { id, ...otherProps } = { id: 1, name: 'John', age: 30 };

Was this answer clear?