Lädt...


🔧 #1 JavaScript Tricks for Cleaner Code with : 💡Object Destructuring


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Welcome

Hello Guys,

Welcome to Top 10 Javascript tricks for clean code

Series #1 : " Object Destructuring "

Its me Md Taqui Imam.
Today In this Blog series #1 i will try to Go in-dept on "Object Destructuring" as possible with examples .

Follow me in Github

So. Let's Get Started🔥.

❓ What is Object Destructuring?

Object destructuring is a cool feature in modern JavaScript that can help make your code cleaner and easier to read. Here are some tips for using object destructuring like a pro!

📚 What to use Object Destructuring?

Destructuring allows you to unpack values from arrays or properties from objects into distinct variables.

For example:

const person = {
  firstName: "John",
  lastName: "Doe",
  age: 30
};

// Without destructuring
const firstName = person.firstName; 
const lastName = person.lastName;
const age = person.age;

// With destructuring
const {firstName, lastName, age} = person;

So with destructuring we can extract multiple properties into variables in one line instead of doing it manually. Pretty sweet!

🍭 Common Uses for Object Destructuring

There are a few handy ways you can use object destructuring to write cleaner code:

1. Extracting function parameters

// Without destructuring 
function getFullName(user) {
  const firstName = user.firstName;
  const lastName = user.lastName;

  return `${firstName} ${lastName}`;
}

// With destructuring
function getFullName({firstName, lastName}) {
  return `${firstName} ${lastName}`;  
}

getFullName({firstName: "John", lastName: "Doe"});

No more repetitive code to extract firstName and lastName!

2. Destructuring return values

// Without destructuring
function calculate() {
  return {result: 10}; 
}

const obj = calculate();
const result = obj.result;

// With destructuring
function calculate() {
  return {result: 10};
}

const {result} = calculate();

Unpack what you need directly into variables.

3. Renaming extracted variables

You can also rename the extracted variables by doing:

const {firstName: fn, lastName: ln} = person;

This reassigns firstName to a variable called fn instead. Helpful for avoiding naming conflicts!

4. Setting default values

You can set default values if the extracted property doesn't exist:

const {middleName = "N/A"} = person;

Now middleName will be "N/A" if it's undefined on person.

🏁 Destructuring is Your Friend!

There are many other neat tricks for destructuring objects and arrays in JavaScript. But these examples should give you a good starter kit to start writing cleaner code.

Give it a try and see how destructuring can tidy up your code !

Bonus 🎉:

10 use cases for JavaScript object destructuring with examples 🎉

1. Assign Variables from Properties

const user = {
  name: 'John Doe',
  age: 30
};

const { name, age } = user;

console.log(name); // John Doe 
console.log(age); // 30

This allows us to extract properties from an object and assign them to variables in one line.

2. Assign with Alias

const user = {
  name: 'John Doe',
  id: 123
};

const { name: userName, id: userId } = user;

console.log(userName); // John Doe
console.log(userId); // 123 

We can alias the properties when destructuring to give them different variable names.

3. Assign Default Values

const user = {
  name: 'John Doe'
};

const { name, age = 30 } = user;

console.log(name); // John Doe
console.log(age); // 30

Default values allow falling back to something if the property is undefined.

4. Nested Objects

const user = {
  name: 'John Doe',
  profile: {
    age: 30,
    location: 'Canada' 
  }
};

const { profile: { age, location } } = user;

console.log(age); // 30 
console.log(location); // Canada

We can access nested properties by destructuring nested objects.

5. Arrays

const [a, b] = [1, 2];

console.log(a); // 1
console.log(b); // 2

We can destructure arrays in the same way as objects.

6. Return Multiple Values

function calc() {
  return {
    sum: 1 + 2,
    product: 3 * 4   
  };
}

const { sum, product } = calc();

console.log(sum); // 3
console.log(product); // 12

Destructuring allows functions to return multiple values which we can assign to variables.

7. Parameter Handling

function add({ a, b }) {
  return a + b;
}

add({ a: 1, b: 2 }); // 3

When calling functions, we can destructure parameters passed in.

8. Omit Properties

const user = { 
  id: 42,
  isAdmin: false,
  name: 'John'
};

const { id, ...userWithoutId } = user;

console.log(id); // 42
console.log(userWithoutId); // {isAdmin: false, name: "John"}

We can omit properties during destructuring.

9. String Destructuring

const [first, second] = 'hello';

console.log(first); // 'h' 
console.log(second); // 'e'

Strings can also be destructured like arrays.

10. Destructure Functions

const add = ({a, b}) => a + b;

add({a: 1, b: 2}); // 3

Function parameters can be destructured for convenient usage.

Thankyou for reading this blog,

i hope you find this helpful and learned something new 😊

*See you next series of Javascript clean Coding Tricks *

Don't Forget to Like 💖🦄🔥

Happy Coding 👋

...

🔧 #1 JavaScript Tricks for Cleaner Code with : 💡Object Destructuring


📈 57.65 Punkte
🔧 Programmierung

🔧 Array Destructuring / Object Destructuring


📈 48.98 Punkte
🔧 Programmierung

🔧 Javascript Array/Object Destructuring - With Some Tricks You Probably Dont Know


📈 41.55 Punkte
🔧 Programmierung

🔧 Mastering JavaScript Object Destructuring: Simplify Your Code with Ease! 🎯Part 3


📈 37.14 Punkte
🔧 Programmierung

🔧 Simplifying JavaScript Code with Object Destructuring


📈 37.14 Punkte
🔧 Programmierung

🔧 JavaScript Object Destructuring


📈 33.96 Punkte
🔧 Programmierung

🔧 JAVASCRIPT - Desestruturação de objetos (object destructuring)


📈 33.96 Punkte
🔧 Programmierung

🔧 Tricky JavaScript Interview Question Using Array And Object Destructuring Combined


📈 33.96 Punkte
🔧 Programmierung

🔧 Tricky JavaScript Interview Question Using Array And Object Destructuring Combined


📈 33.96 Punkte
🔧 Programmierung

🔧 Array Destructuring in JavaScript: Tips, Tricks, and Techniques


📈 33.43 Punkte
🔧 Programmierung

🔧 Mastering Object.freeze() and Object.seal() in JavaScript: Controlling Object Mutability


📈 29.77 Punkte
🔧 Programmierung

🔧 Why JavaScript Says "[object Object]" and Not Just "[object]" 🤔


📈 29.77 Punkte
🔧 Programmierung

🔧 6 TOP JAVASCRIPT TRICKS FOR CLEANER CODE


📈 29.1 Punkte
🔧 Programmierung

🔧 How Destructuring Works in JavaScript – Explained with Code Examples


📈 29.02 Punkte
🔧 Programmierung

🔧 Simplifying JavaScript Code with Array Destructuring


📈 29.02 Punkte
🔧 Programmierung

🔧 Object destructuring in TypeScript


📈 28.55 Punkte
🔧 Programmierung

🍏 AnyMP4 iOS Cleaner 1.0.18 - iOS cleaner.


📈 25.85 Punkte
🍏 iOS / Mac OS

🍏 App Cleaner & Uninstaller 8.1 - Preview and remove applications and their service files (was App Cleaner).


📈 25.85 Punkte
🍏 iOS / Mac OS

📰 'Say hello to my little vacuum cleaner!' US drug squad puts spycams in cleaner's kit


📈 25.85 Punkte
📰 IT Security Nachrichten

📰 'Say hello to my little vacuum cleaner!' US drug squad puts spycams in cleaner's kit


📈 25.85 Punkte
📰 IT Security Nachrichten

🔧 Destructuring Assignment di JavaScript: Biar Ngoding Makin Kece!


📈 25.85 Punkte
🔧 Programmierung

🔧 JavaScript - Destructuring Arrays & Objects [Live Doc]


📈 25.85 Punkte
🔧 Programmierung

🔧 JavaScript: Default Parameters, Spread Operator, Rest Parameters, and Destructuring!


📈 25.85 Punkte
🔧 Programmierung

🔧 🧐 JavaScript Shenanigans: Time Travel with Destructuring


📈 25.85 Punkte
🔧 Programmierung

🔧 Exploring Destructuring in JavaScript


📈 25.85 Punkte
🔧 Programmierung

🔧 JavaScript destructuring


📈 25.85 Punkte
🔧 Programmierung

🔧 Destructuring Assignments in JavaScript


📈 25.85 Punkte
🔧 Programmierung

🔧 Array Destructuring no Javascript, você sabe o que é?


📈 25.85 Punkte
🔧 Programmierung

🔧 Advanced JavaScript Operators – Nullish Coalescing, Optional Chaining, and Destructuring Assignment


📈 25.85 Punkte
🔧 Programmierung

🔧 Mastering Destructuring in JavaScript: Simplify Arrays and Objects


📈 25.85 Punkte
🔧 Programmierung

🔧 JavaScript Destructuring.


📈 25.85 Punkte
🔧 Programmierung

🔧 Demystifying Destructuring Assignment in JavaScript


📈 25.85 Punkte
🔧 Programmierung

🔧 JavaScript Destructuring.


📈 25.85 Punkte
🔧 Programmierung

🔧 JavaScript Array Destructuring


📈 25.85 Punkte
🔧 Programmierung

matomo