Please enable JavaScript.
Coggle requires JavaScript to display documents.
Destructuring assignment - Coggle Diagram
Destructuring assignment
define
Destructuring assignment is a special syntax that allows us to “unpack” arrays or objects into a bunch of variables
Array destructuring
-
-
-
-
Looping with .entries()
for (let [key, value] of Object.entries(user)) {}
Map iterates as [key, value] pairs
for (let [key, value] of user) {}
Swap variables trick
[guest, admin] = [admin, guest];
-
The rest ‘…’
let [name1, name2, ...rest] = ["Julius", "Caesar", "Consul", "of the Roman Republic"];
-
Default values
-
assing default
let [name = "Guest", surname = "Anonymous"] = ["Julius"];
-
Object destructuring
syntax
let {var1, var2} = {var1:…, var2:…}
-
-
-
-
-
-
The rest pattern “…”
let {title, ...rest} = options;
Gotcha if there’s no let
let title, width, height;
({title, width, height} = {title: "Menu", width: 200, height: 100});
To show JavaScript that it’s not a code block, we can wrap the expression in parentheses (...):
-
-