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 DestructuringArray 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); OutputExample 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); Outputa [ '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); Output20 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); Outputa 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); Output21 -34 47 Object destructuringSimple 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 Output10 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} Output10 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); Output15 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); OutputGFG India JS destructuring Comment More infoAdvertise with us Next Article Destructuring in JavaScript R ranadeepika2409 Follow 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 What is Destructuring in ES6? Destructuring in JavaScript allows you to extract values from arrays or objects and assign them to variables in a concise and readable way. It simplifies code, making it shorter and easier to understand.How Destructuring worksArray Destructuring: It lets you easily grab values from an array and assi 4 min read JavaScript get Function JavaScript get function is used to access the properties of an object using dot notation or square brackets. It allows you to retrieve the value associated with a particular property key and the get function is often used when working with objects that implement JavaScript's getter function. The get 3 min read Introduction to JavaScript JavaScript is a versatile, dynamically typed programming language used for interactive web applications, supporting both client-side and server-side development, and integrating seamlessly with HTML, CSS, and a rich standard library.JavaScript is a single-threaded language that executes one task at 7 min read JavaScript Function Examples A function in JavaScript is a set of statements that perform a specific task. It takes inputs, and performs computation, and produces output. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different 3 min read JavaScript Code Execution JavaScript is a synchronous (Moves to the next line only when the execution of the current line is completed) and single-threaded (Executes one command at a time in a specific order one after another serially) language. To know behind the scene of how JavaScript code gets executed internally, we hav 4 min read Like