← Back to JavaScript Course | Chapter 5: ES6+ Features | Lesson 1 of 12

JS स्प्रेड ऑपरेटर

Spread operator (तीन dots) किसी list या object के items को unpack करता है ताकि आप उन्हें किसी दूसरे में डाल सकें, जैसे crayons के एक box को एक बड़े box में खाली करना। यह copy करने और combine करने के लिए काम आता है।
Syntax
javascript
const newArray = [...array1, ...array2];
const newObject = { ...object1, ...object2 };
functionName(...array);

Spread Syntax क्या है

Spread operator ... किसी iterable जैसे array या string को उसके individual elements में फैला देता है, जहाँ भी कई values की उम्मीद हो, जैसे किसी array literal या function call के अंदर।

उदाहरण: What Is Spread Syntax

javascript
const nums = [1, 2, 3];
console.log(...nums); // expands array into individual values
console.log([...nums]);

Arrays को Combine करना

दो या ज़्यादा arrays को एक नए array literal में spread करना, जैसे [...a, ...b], उनके elements को बिना किसी original array को mutate किए एक नई single array में जोड़ देता है, और आपको mix में कहीं भी अतिरिक्त values डालने देता है।

उदाहरण: Combining Arrays

javascript
// Declare the constant `a`, set to `[1, 2]`
// Declare the constant `a`, set to `[1, 2]`
const a = [1, 2];
// Declare the constant `b`, set to `[3, 4]`
// Declare the constant `b`, set to `[3, 4]`
const b = [3, 4];
// Declare the constant `combined`, set to `[...a, ...b, 5]`
// Declare the constant `combined`, set to `[...a, ...b, 5]`
const combined = [...a, ...b, 5];
// Print `combined` to the console
// Print `combined` to the console
console.log(combined);

Objects को Copy करना

{...obj} से किसी object को spread करना उसकी अपनी enumerable properties को एक नए object में copy कर देता है, यह original को छुए बिना एक updated copy बनाने का एक आम pattern है, जिसे अक्सर खास fields को override करने के लिए नई properties के साथ मिलाया जाता है।

उदाहरण: Copying Objects

javascript
// Declare the constant `obj`, set to `{ name: "Sam", age: 30 }`
// Declare the constant `obj`, set to `{ name: "Sam", age: 30 }`
const obj = { name: "Sam", age: 30 };
// Declare the constant `copy`, set to `{ ...obj, age: 31 }`
// Declare the constant `copy`, set to `{ ...obj, age: 31 }`
const copy = { ...obj, age: 31 };
// Print `copy` to the console
// Print `copy` to the console
console.log(copy);

Function Calls के साथ Spread

Spread function calls के अंदर भी काम करता है, [1, 2, 3] जैसी किसी array को तीन अलग-अलग arguments में बदल देता है, इसलिए Math.max(...numbers) array को खुद पास करने के बजाय हर number को अलग-अलग पास करता है।

उदाहरण: Spread with Function Calls

javascript
const numbers = [1, 5, 3];
console.log(Math.max(...numbers));

Shallow Copy की सीमा

Spread सिर्फ एक level गहराई तक copy करता है, इसलिए spread की गई value के अंदर nested objects या arrays reference से copy होते हैं, यानी किसी nested object में बदलाव original और copy दोनों को प्रभावित करते हैं।

उदाहरण: Shallow Copy Limitation

javascript
const original = { info: { city: "Delhi" } };
const copy = { ...original };
copy.info.city = "Mumbai";
console.log(original.info.city); // "Mumbai" - nested object is shared
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि spread एक deep copy बनाता है, जबकि {...obj} सिर्फ top level copy करता है और nested objects अभी भी shared रहते हैं।
  2. किसी non-iterable को array में spread करना, जैसे [...5], जो TypeError throw करता है।
  3. किसी object में spread से पहले properties रखना, जैसे {a: 1, ...obj}, इसलिए obj में मौजूद value आपकी पहले वाली को override कर देती है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.