Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
Destructuring in JavaScript
Next article icon

Destructuring in JavaScript

Last Updated : 09 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Destructuring Assignment is a JavaScript expression that allows to unpack of values from arrays, or properties from objects, into distinct variables data can be extracted from arrays, objects, and nested objects, and assigned to variables.

Array Destructuring

Array members can be unpacked into different variables. The following are different examples.

JavaScript
const a = [10, 20, 30, 40]  console.log("Example 1"); const [x, y, z, w] = a; console.log(x); console.log(y); console.log(z); console.log(w);  const [p, q, , r] = a; console.log("Example 2"); console.log(p); console.log(q); console.log(r);  const [s, t] = a; console.log("Example 3"); console.log(s); console.log(t); 

Output
Example 1 10 20 30 40 Example 2 10 20 40 Example 3 10 20 

Example with Rest Operator : In order to assign some array elements to variable and rest of the array elements to only a single variable can be achieved by using rest operator (...) as in below implementation. But one limitation of rest operator is that it works correctly only with the last elements implying a subarray cannot be obtained leaving the last element in the array. 

javascript
let [fst, , ...last] = ["a", "b", "c", "d"];  console.log(fst); console.log(last); 

Output
a [ 'c', 'd' ] 

Example of Swapping : Values can be swapped using destructuring assignment as below: 

javascript
let x = 10, y = 20;  // Swapping  [x, y] = [y, x];  console.log(x); console.log(y); 

Output
20 10 

Example of Function Return: Data can also be extracted from an array returned from a function. One advantage of using a destructuring assignment is that there is no need to manipulate an entire object in a function but just the fields that are required can be copied inside the function. 

javascript
function NamesList() {     return ["a", "b", "c", "d"] }  let [fst, snd] = NamesList();  console.log(fst); console.log(snd); 

Output
a b 

Example 6: In ES5 to assign variables from objects its implementation is 

javascript
let marks = { x: 21, y: -34, z: 47 };  const { x, y, z } = marks;  console.log(x); console.log(y); console.log(z);  

Output
21 -34 47 

Object destructuring

Simple Object destructuring : In the below examplex, properties (and their values) of an object are assigned to variables.

JavaScript
({ x, y} = { x: 10, y: 20 }); console.log(x); // 10 console.log(y); // 20 

Output
10 20 
JavaScript
({x, y, ...restof} = {x: 10, y: 20, m: 30, n: 40}); console.log(x); // 10 console.log(y); // 20 console.log(restof); // {m: 30, n: 40} 

Output
10 20 { m: 30, n: 40 } 

Nesyed Object destructuring : The Nested objects can also be destructured using destructuring syntax. 

javascript
const marks = {     section1: { alpha: 15, beta: 16 },     section2: { alpha: -31, beta: 19 } }; const { section1: { alpha: alpha1, beta: beta1 } } = marks; console.log(alpha1, beta1); 

Output
15 16 
JavaScript
let obj = {     name: "GFG",     add: {         country: "India",         state: {             code: "JS",             pincode: "820800",             article: {                 topic: "destructuring"             }         }     } }  let { name } = obj; console.log(name)  let { add: { country: abcd } } = obj console.log(abcd)  let { add: { state: { code: cd } } } = obj console.log(cd)  let { add: { state: { article: { topic: ef } } } } = obj console.log(ef); 

Output
GFG India JS destructuring 

Next Article
Destructuring in JavaScript

R

ranadeepika2409
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Questions

Similar Reads

    Debouncing in JavaScript
    In JavaScript, debouncing is a technique used to ensure that a function is not called too frequently. It is commonly used in scenarios where events are triggered rapidly, such as typing in an input field or resizing a window. Without debouncing, functions might be executed many times in quick succes
    6 min read
    Abstraction in JavaScript
    In JavaScript, Abstraction can be defined as the concept of hiding the inner complex workings of an object and exposing only the essential features to the user. Hiding Complexity: Implementation is hidden, it shows only the necessary details.Modularity: Code is organized in a reusable form, which im
    4 min read
    Debugging in JavaScript
    Debugging is the process of testing, finding, and reducing bugs (errors) in computer programs. It involves:Identifying errors (syntax, runtime, or logical errors).Using debugging tools to analyze code execution.Implementing fixes and verifying correctness.Types of Errors in JavaScriptSyntax Errors:
    4 min read
    Dequeue in JavaScript
    A Deque (Double-Ended Queue) in JavaScript is a flexible data structure that functions like a horizontal container with both ends open, allowing data insertion and removal from both the front and the rear.Deques provide efficient access and modifications at both ends, reducing time complexity in cer
    6 min read
    JavaScript Function() Constructor
    The JavaScript Function() constructor is used to create new function objects dynamically. By using the Function() constructor with the new operator, developers can define functions on the fly, passing the function body as a string. This allows for greater flexibility in situations where functions ne
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences