
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Is it possible to de-structure to already-declared variables? In JavaScript?
Following is the code to de-structure already declared variables in JavaScript −
Example
<!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; } .result { font-size: 18px; font-weight: 500; color: rebeccapurple; } </style> </head> <body> <h1>De-structure to already-declared variables</h1> <div class="result"></div> <br /> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to destructure the personObj object</h3> <script> let resEle = document.querySelector(".result"); let BtnEle = document.querySelector(".Btn"); let name, age, personObj; personObj = { name: "Rohan", age: 19, }; BtnEle.addEventListener("click", () => { ({ name, age } = personObj); resEle.innerHTML = " name = " + name + "<br>age = " + age; }); </script> </body> </html>
Output
The above code will produce the following output −
On clicking the ‘CLICK HERE’ button −
- Related Articles
- How to de-structure an imported object in JavaScript?
- How to access variables declared in a function, from another function using JavaScript?
- Is it possible to write to MongoDB console in JavaScript execution?
- Is it possible to change the HTML content in JavaScript?
- Can I use a JavaScript variable before it is declared?
- Why variables are declared final in Java
- Is it possible to select text boxes with JavaScript?
- Is it possible to write data to file using only JavaScript?
- Is it possible to display substring from object entries in JavaScript?
- Is it possible to create a new data type in JavaScript?
- Is it possible to have JavaScript split() start at index 1?
- C program to compare the structure variables
- Is it possible to change the value of the function parameter in JavaScript?
- Is it possible to change values of the array when doing foreach() in javascript?
- Is it possible to use Python modules in Octave?

Advertisements