Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 7 of 10 · Functions and Arrow Functions
Interview question

What are rest parameters and the spread operator? Rest parameters और spread operator क्या हैं?

Answer

Rest parameters (...) allow you to collect multiple arguments into an array. The spread operator (...) does the opposite - it expands an array or object into individual elements.

// Rest parameters - collect into array
function sum(...numbers) {
    return numbers.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3, 4, 5)); // 15

// Rest with named parameters
function greet(greeting, ...names) {
    console.log(greeting + ' ' + names.join(', '));
}
greet('Hello', 'John', 'Jane', 'Bob'); // 'Hello John, Jane, Bob'

// Spread with arrays
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5]; // [1, 2, 3, 4, 5]

const max = Math.max(...[1, 5, 3, 2]); // equivalent to Math.max(1, 5, 3, 2)
console.log(max); // 5

// Spread with objects
const user1 = { name: 'John', age: 30 };
const user2 = { ...user1, email: 'john@gmail.com' };
// { name: 'John', age: 30, email: 'john@gmail.com' }

// Spread to merge objects
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 }; // b will be overwritten
const merged = { ...obj1, ...obj2 }; // { a: 1, b: 3, c: 4 }

// Rest in arrow functions
const multiply = (...nums) => nums.reduce((a, b) => a * b, 1);
console.log(multiply(2, 3, 4)); // 24

// Difference: Rest collects, Spread expands
const nums = [1, 2, 3];

// Spread: expand array
function test(a, b, c) {
    console.log(a, b, c);
}
test(...nums); // 1, 2, 3

// Rest: collect arguments
function collect(...args) {
    console.log(args); // [1, 2, 3]
}
collect(1, 2, 3);

Interview tip: Rest parameters must be the last parameter in the function signature. Spread operator can be used with arrays, objects, function calls, and function parameters. They're ES6 features that enable more flexible code.

Rest parameters (...) multiple arguments को एक array में collect करते हैं। Spread operator (...) opposite करता है - एक array या object को individual elements में expand करता है।

// Rest - collect into array
function sum(...numbers) {
    return numbers.reduce((a, b) => a + b, 0);
}
console.log(sum(1, 2, 3, 4)); // 10

// Named parameters के साथ
function greet(greeting, ...names) {
    console.log(greeting + ' ' + names.join(', '));
}
greet('Hello', 'John', 'Jane'); // 'Hello John, Jane'

// Spread with arrays
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5]; // [1, 2, 3, 4, 5]

const max = Math.max(...[1, 5, 3]); // Math.max(1, 5, 3)

// Spread with objects
const user1 = { name: 'John', age: 30 };
const user2 = { ...user1, email: 'john@gmail.com' };

// Objects को merge करना
const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 };
const merged = { ...obj1, ...obj2 }; // { a: 1, b: 3, c: 4 }

// Arrow functions में rest
const multiply = (...nums) => nums.reduce((a, b) => a * b, 1);

// Rest: collect
function collect(...args) {
    console.log(args); // [1, 2, 3]
}
collect(1, 2, 3);

इंटरव्यू टिप: Rest parameters function signature का last parameter होना चाहिए। Spread operator को arrays, objects, function calls, और function parameters के साथ use कर सकते हैं। ES6 features हैं जो flexible code enable करते हैं।

Was this answer clear?