--- id: javascript-destructuring title: "JavaScript Destructuring" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["destructuring", "JS destructuring", "destructuring assignment", "object destructuring", "array destructuring", "rest property"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.89 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "destructuring", "es6"] raw_sources: ["https://www.w3schools.com/js/js_destructuring.asp"] applied_in: [] github_commit: "" --- # [[JavaScript Destructuring]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Destructuring assignment unpacks objects and arrays (and any iterable) into individual variables without mutating the original β€” supporting defaults, aliases, skipping, position picks, and a rest property. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Unpacks objects into variables** β€” The destructuring assignment syntax can unpack objects into variables. [S1] - **Order-independent for objects** β€” When destructuring objects, the order of the properties does not matter. [S1] - **Non-destructive** β€” Destructuring is not destructive; it does not change the original object. [S1] - **Default values** β€” For potentially missing properties you can set default values. [S1] - **Property aliases** β€” A destructured property can be renamed into a different variable name. [S1] - **Works on any iterable** β€” Destructuring can be used with any iterables, including strings. [S1] - **Array picks and skips** β€” You can pick array variables, skip values with extra commas, and pick by specific index. [S1] - **Rest property** β€” Ending a destructuring with a rest property stores all remaining values into a new array. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **`{a, b} = obj`** β€” Object destructuring binds by property name, in any order. [S1] - **`{x = default}`** β€” Supply defaults inline for properties that may be missing. [S1] - **`{prop : alias}`** β€” Rename a property into a new variable. [S1] - **`[a,,,b]`** β€” Use extra commas to skip array positions. [S1] - **`{[0]:x ,[1]:y}`** β€” Pick array values by specific index. [S1] - **`[a, b, ...rest]`** β€” Collect remaining array values into `rest`. [S1] - **`[a, b] = [b, a]`** β€” Swap two variables in one statement. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Destructuring Assignment Syntax** The destructuring assignment syntax can unpack objects into variables: [S1] ```javascript let {firstName, lastName} = person; ``` **Object Destructuring** [S1] ```javascript // Create an Object const person = { firstName: "John", lastName: "Doe", age: 50 }; // Destructuring let {firstName, lastName} = person; ``` The order of the properties does not matter: [S1] ```javascript // Create an Object const person = { firstName: "John", lastName: "Doe", age: 50 }; // Destructuring let {lastName, firstName} = person; ``` Destructuring is not destructive. Destructuring does not change the original object. [S1] **Object Default Values** β€” For potentially missing properties we can set default values: [S1] ```javascript // Create an Object const person = { firstName: "John", lastName: "Doe", age: 50 }; // Destructuring let {firstName, lastName, country = "US"} = person; ``` **Object Property Alias** [S1] ```javascript // Create an Object const person = { firstName: "John", lastName: "Doe", age: 50 }; // Destructuring let {lastName : name} = person; ``` **String Destructuring** β€” One use for destructuring is unpacking string characters. Destructuring can be used with any iterables. [S1] ```javascript // Create a String let name = "W3Schools"; // Destructuring let [a1, a2, a3, a4, a5] = name; ``` **Array Destructuring** β€” We can pick up array variables into our own variables: [S1] ```javascript // Create an Array const fruits = ["Bananas", "Oranges", "Apples", "Mangos"]; // Destructuring let [fruit1, fruit2] = fruits; ``` **Skipping Array Values** β€” We can skip array values using two or more commas: [S1] ```javascript // Create an Array const fruits = ["Bananas", "Oranges", "Apples", "Mangos"]; // Destructuring let [fruit1,,,fruit2] = fruits; ``` **Array Position Values** β€” We can pick up values from specific index locations of an array: [S1] ```javascript // Create an Array const fruits = ["Bananas", "Oranges", "Apples", "Mangos"]; // Destructuring let {[0]:fruit1 ,[1]:fruit2} = fruits; ``` **The Rest Property** β€” You can end a destructuring syntax with a rest property. This syntax will store all remaining values into a new array: [S1] ```javascript // Create an Array const numbers = [10, 20, 30, 40, 50, 60, 70]; // Destructuring const [a,b, ...rest] = numbers ``` **Destructuring Maps** [S1] ```javascript // Create a Map const fruits = new Map([ ["apples", 500], ["bananas", 300], ["oranges", 200] ]); // Destructuring let text = ""; for (const [key, value] of fruits) { text += key + " is " + value; } ``` **Swapping JavaScript Variables** β€” You can swap the values of two variables using a destructuring assignment: [S1] ```javascript let firstName = "John"; let lastName = "Doe"; // Destructuring [firstName, lastName] = [lastName, firstName]; ``` ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own snippets are the canonical applied examples β€” object/array destructuring, defaults, aliases, string and Map iteration, the rest property, and the variable-swap idiom. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Object destructuring with default and alias (language: JavaScript): ```javascript let {firstName, lastName, country = "US"} = person; let {lastName : name} = person; ``` Rest property collects the remainder: ```javascript const [a, b, ...rest] = numbers; ``` Swap two variables: ```javascript [firstName, lastName] = [lastName, firstName]; ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.89 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript Object Types Note]], [[JavaScript Type Conversion]], [[JavaScript Introduction]], [[JavaScript NaN]] - **μ°Έμ‘° λ§₯락:** Referenced whenever extracting fields from objects/arrays, setting defaults, or swapping variables in modern (ES6+) JavaScript. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JavaScript Destructuring β€” https://www.w3schools.com/js/js_destructuring.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JavaScript Destructuring" page (Astra wiki-curation, P-Reinforce v3.1 format).