Skip to content

Hey everyone! πŸ‘‹ I'm diving headfirst into a 100-day JavaScript adventure, and I couldn't be more thrilled to share it with you all! πŸŽ‰ Over the next three months, I'll be immersing myself in everything JavaScript has to offer, from the very basics to some seriously advanced concepts.

Notifications You must be signed in to change notification settings

lassiecoder/100daysofjs

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

32 Commits
Β 
Β 

Repository files navigation

Destructuring assignment for efficient coding

πŸ₯‘ Array destructuring

πŸ₯‘ Object destructuring

πŸ₯‘ Nested destructuring

πŸ₯‘ Smart function parameters


Destructuring assignment simplifies variable assignment by extracting values from arrays or objects and assigning them to variables, enhancing code readability.

Array destructuring

Array destructuring allows you to unpack values from arrays into distinct variables using a syntax that resembles array literals.

// Example 1: Basic array destructuring
let numbers = [1, 2, 3];
let [a, b, c] = numbers;
console.log(a); // Output: 1
console.log(b); // Output: 2
console.log(c); // Output: 3

// Example 2: Skipping elements
let [x, , z] = numbers;
console.log(x); // Output: 1
console.log(z); // Output: 3

Object destructuring

Object destructuring enables you to extract properties from objects and assign them to variables with the same name.

For example:

// Example 1: Basic object destructuring
let person = { name: 'John', age: 30 };
let { name, age } = person;
console.log(name); // Output: John
console.log(age); // Output: 30

// Example 2: Renaming variables
let { name: personName, age: personAge } = person;
console.log(personName); // Output: John
console.log(personAge); // Output: 30

Nested destructuring

You can also destructure nested arrays and objects.

For example:

// Example: Nested array destructuring
let nestedArray = [1, [2, 3], 4];
let [first, [second, third], fourth] = nestedArray;
console.log(first); // Output: 1
console.log(second); // Output: 2
console.log(third); // Output: 3
console.log(fourth); // Output: 4

// Example: Nested object destructuring
let nestedObject = { outer: { inner: 'value' } };
let { outer: { inner } } = nestedObject;
console.log(inner); // Output: value

Smart function parameters

Destructuring can also be used in function parameters to extract values from objects directly.

For example:

// Example:
function greet({ name, age }) {
  console.log(`Hello, ${name}! You are ${age} years old.`);
}

let person = { name: 'Alice', age: 25 };
greet(person); // Output: Hello, Alice! You are 25 years old.

Destructuring assignment is a concise way to extract values from arrays or objects and assign them to variables.

For objects, the syntax is:

let {prop: varName = defaultValue, ...rest} = object;

This assigns the value of the property prop to the variable varName, with an optional default value if the property doesn't exist. Any remaining properties are collected into the rest object.

For arrays, the syntax is:

let [item1 = defaultValue, item2, ...rest] = array;

This assigns the first item to item1, the second to item2, and collects any remaining items into the rest array.

Nested arrays or objects can also be destructured, as long as the structure on the left matches that on the right.

About

Hey everyone! πŸ‘‹ I'm diving headfirst into a 100-day JavaScript adventure, and I couldn't be more thrilled to share it with you all! πŸŽ‰ Over the next three months, I'll be immersing myself in everything JavaScript has to offer, from the very basics to some seriously advanced concepts.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published