session Storage In JavaScript In Hindi | JS session Storage In Hindi
JavaScript eval() Function In Hindi | JS eval() Function
JavaScript Object keys, values, entries : Object methods in JS
JavaScript Event Bubbling Example | Event Bubbling In JS In Hindi
what are callback functions in JavaScript
aws s3 kya hai ? | what is aws s3
Selection Sort in PHP With Example | How does selection sort work in PHP ?
Linux adduser Command In Hindi
AWS S3-Snowflake Pipeline Example | Snowflake Tutorials In Hindi
Laravel Service Container Example | Service Container In Laravel In HIndi
If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Normally JavaScript में Array या Object को merge करने के लिए हम कोई method या for Loop का use करते हैं , लेकिन JavaScript में हमारे पास spread syntax होता है , जिसकी help से Array या Object को merge करने के अलावा काफी काम कर सकते हैं।
तो जैसा कि नाम है spread , मतलब फैलाना
और नाम के according ही इसका काम भी है। तो spread Syntax का use किसी Array या Object से values को unpack करने के लिए किया है। unpack का मतलब है values को get करना।
इसे spread Operator भी कहते हैं। और use करने लिए triple dots (...var)
का use किया जाता है।
तो सबसे पहले हम एक Array को merge करने का example देख लेते हैं।
let group1 = ["Raju", "Shaym"];
let group2 = ["Babu Rao"];
// now merge them .
let all = [...group1, ...group2];
console.log(all);
Output :
Array(3) [ "Raju", "Shaym", "Babu Rao" ]
So , basically spread operator , array में available values को unpack करता है जैसे हम normal variable में कोई value save करते हैं। अब हालाँकि Example में हमें फिर से वो ही values को array रख दिया है इसलिए हमें final output एक Array मिला।
इसे आप ऐसे समझ सकते हैं -
let all = [...group1, ...group2];
/*
group1 = Raju, Shyam
group2 = Babu Rao
So, [...group1, ...group2] = [Raju, Shyam, Babu Rao]
*/
हालाँकि values आपको normal variable की तरह ही मिलेगी अगर आप इन्हे console में print करोगे तो कुछ ऐसा मिलेगा।
console.log(...group1 , ...group2);
// Output : Raju Shaym Babu Rao
ध्यान रहे , spread Syntax और rest Syntax दोनों ही triple dots का use करते हैं लेकिन working different है। spread Syntax जहाँ values को spread करता है तो rest Syntax सभी values को single element में group करता है , जैसे कि Variable length Argument Functions
ठीक इसी तरह से आप Objects को भी एक single Object में merge कर सकते हैं।
let info = {fname : 'Rahul', lname:'Rajput'};
let other_info = {address : 'UP, India'};
let links = {website : 'learnhindituts.com', youtube:'@leanrhindituts2221'};
// now merge them .
let user_info = {...info, ...other_info, ...links};
console.log(user_info);
Output :
Object { fname: "Rahul", lname: "Rajput", address: "UP, India", website: "learnhindituts.com", youtube: "@leanrhindituts2221" }
Spread Syntax का use आप किसी functions में values को pass करते time भी कर सकते हैं।
Example :
function sum(num1, num2, num3) {
console.log("Sum : ", (num1 + num2 + num3));
}
let nums = [20, 30, 40];
sum(...nums);let nums = [20, 30, 40];
let obj = {...nums};
console.log(obj);
Output :
Sum : 90
सिर्फ Iterable Object जैसे Array को ही आप किसी Array या function parameters में spread कर सकते हैं , Object को नहीं।
Example :
let nums = {num1:12, num2:23}; sum(...nums); // Error : Uncaught TypeError: nums is not iterable
हालाँकि किसी Array को Object में spread कर सकते हैं ऐसे में सभी indexes Object की property बन जाएगी , जैसे -
let nums = [20, 30, 40];
let obj = {...nums};
console.log(obj);
Output :
Object { 0: 20 1: 30 2: 40 }
I Hope , अब आपको JavaScript में Spread Syntax में समझ आ गया होगा।
Loading ...
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook