डीस्ट्रक्चरिंग असाइनमेंट एक विशेष सिंटैक्स है जो हमें ऑब्जेक्ट प्रॉपर्टीज या एरे वैल्यूज को डीस्ट्रक्चर करके अलग-अलग वेरिएबल में असाइन करने की अनुमति देता है।
जावास्क्रिप्ट में डीस्ट्रक्चरिंग असाइनमेंट के लिए कोड निम्नलिखित है:
उदाहरण
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample,.result { font-size: 18px; font-weight: 500; color: red; } .result { color: rebeccapurple; } </style> </head> <body> <h1>De-structuring assignment in JavaScript</h1> <div class="sample"></div> <div class="result"></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to destructure the array and object above</h3> <script> let sampleEle = document.querySelector(".sample"); let resultEle = document.querySelector(".result"); let arr = [2, 3, 4, 5]; let obj = { firstName: "Rohan", lastName: "Sharma", }; sampleEle.innerHTML = "arr = " + arr + "<br>"; sampleEle.innerHTML += "obj = {firstName:'Rohan',lastName:'Sharma'}"; let a, b; [a, b] = arr; let { firstName, lastName } = obj; document.querySelector(".Btn").addEventListener("click", () => { resultEle.innerHTML = `a=${a} b=${b} <br>`; resultEle.innerHTML += `firstName=${firstName} lastName=${lastName}`; }); </script> </body> </html>
आउटपुट
उपरोक्त कोड निम्नलिखित आउटपुट उत्पन्न करेगा:

'CLICK HERE' बटन पर क्लिक करने पर:
