ā¤ Like
šŸ”– Save
šŸ”— Share
@thedevspaceio
@thedevspaceio

JavaScript Destructuring Cheatsheet

@thedevspaceio

🧩 JavaScript Destructuring Cheatsheet

Destructuring lets you unpack values from arrays and properties from objects into variables, with a clean and readable syntax.

āœ… Array destructuring āœ… Object destructuring āœ… Default values āœ… Renaming variables āœ… Nested destructuring āœ… Rest pattern āœ… Function parameters

#javascript #destructuring #arrays #objects #es6 #syntax #webdev #frontend #coding #tips


Destructuring is a way to extract values from arrays and objects into individual variables.

Array destructuring

Assign array elements to variables by position.

js
const colors = ["red", "green", "blue"];
const [first, second] = colors;
 
console.log(first); // "red"
console.log(second); // "green"

Array destructuring


You can skip elements with commas.

js
const [one, , three] = [1, 2, 3];
console.log(one); // 1
console.log(three); // 3

Skip element


Object destructuring

Assign object properties to variables by their names.

js
const user = { name: "Ada", age: 30, role: "admin" };
const { name, role } = user;
 
console.log(name); // "Ada"
console.log(role); // "admin"

Object destructuring


Default values

Provide fallback values when a property is undefined.

js
const settings = { theme: "dark" };
const { theme, fontSize = "16px" } = settings;
 
console.log(theme); // "dark"
console.log(fontSize); // "16px"


Renaming variables

Rename variables while destructuring.

js
const person = { firstName: "Grace", lastName: "Hopper" };
const { firstName: first, lastName: last } = person;
 
console.log(first); // "Grace"
console.log(last); // "Hopper"


Nested destructuring

Unpack values from nested structures.

js
const product = {
  title: "Laptop",
  specs: {
    ram: "16GB",
    storage: "512GB",
  },
};
 
const {
  title,
  specs: { ram },
} = product;
 
console.log(title); // "Laptop"
console.log(ram); // "16GB"


Rest parameter

Collect remaining elements or properties into a new array or object using a rest parameter.

js
const numbers = [1, 2, 3, 4, 5];
const [first, second, ...rest] = numbers;
 
console.log(first); // 1
console.log(second); // 2
console.log(rest); // [3, 4, 5]


Function parameters

Destructuring makes function arguments clearer.

js
function greet({ name, greeting = "Hello" }) {
  return `${greeting}, ${name}!`;
}
 
greet({ name: "Grace", greeting: "Hi" }); // "Hi, Grace!"

Full-Stack AI Developer Roadmap

From HTML & CSS to working with AI models, all in one structured roadmap.

@thedevspaceio
www.thedevspace.io