DEV Community

Cover image for Mastering JS Shorthand Techniques Part-4: Array & Object Destructuring and Speed Operator
ABIDULLAH786
ABIDULLAH786

Posted on • Edited on • Originally published at devwebbytes.blogspot.com

Mastering JS Shorthand Techniques Part-4: Array & Object Destructuring and Speed Operator

This blog is originally published on my Blog website, where you can find the full version with detailed insights and examples. Click the link below to read the complete article and explore more tech-related content!
πŸ‘‰ Click Here

Introduction

As a JavaScript developer, you're likely familiar with the importance of writing clean and efficient code. One way to achieve this is by using shorthand techniques, which can significantly simplify your code and make it more readable. In this blog, we'll explore some powerful shorthand techniques in JavaScript that will help you write code faster and with greater clarity. From destructuring assignments to spread syntax, these techniques will level up your coding skills and boost your productivity.

1. Destructuring Assignment

Destructuring assignment unpacks values from arrays or properties from objects into distinct variables, making it easier to work with complex data structures.

// Longhand
const person = { firstName: 'John', lastName: 'Doe' };
const firstName = person.firstName;
const lastName = person.lastName;

// Shorthand
const person = { firstName: 'John', lastName: 'Doe' };
const { firstName, lastName } = person;
Enter fullscreen mode Exit fullscreen mode

2. Array Destructuring

Array destructuring allows you to extract individual elements from an array and assign them to separate variables.

// Longhand
const numbers = [1, 2, 3];
const firstNumber = numbers[0];
const secondNumber = numbers[1];

// Shorthand
const numbers = [1, 2, 3];
const [firstNumber, secondNumber] = numbers;
Enter fullscreen mode Exit fullscreen mode

3. Object Destructuring:

Similar to array destructuring, object destructuring unpacks object properties into variables, simplifying property access.

// Longhand
const person = {
  firstName: 'John',
  lastName: 'Doe',
};
const firstName = person.firstName;
const lastName = person.lastName;

// Shorthand
const { firstName, lastName } = {
  firstName: 'John',
  lastName: 'Doe',
}
Enter fullscreen mode Exit fullscreen mode

4. Object Shortand

Object shorthand simplifies the process of creating objects with properties that have the same name as variables.

// Longhand
const name = 'John';
const age = 30;
const person = { name: name, age: age };

// Shorthand
const name = 'John';
const age = 30;
const person = { name, age };
Enter fullscreen mode Exit fullscreen mode

5. Array Spread

The array spread operator can be used to create a new array by merging existing arrays or adding elements to an array.

// Longhand
const numbers1 = [1, 2, 3];
const numbers2 = [4, 5, 6];
const mergedArray = numbers1.concat(numbers2);

// Shorthand
const numbers1 = [1, 2, 3];
const numbers2 = [4, 5, 6];
const mergedArray = [...numbers1, ...numbers2];
Enter fullscreen mode Exit fullscreen mode

6. Spread Syntax

The spread syntax (...) unpacks elements from arrays or properties from objects into new arrays or objects. It simplifies tasks like array cloning, merging, and function arguments.

// Longhand
const numbers = [1, 2, 3];
const clone = numbers.slice();
const mergedArray = numbers.concat([4, 5, 6]);

// Shorthand
const numbers = [1, 2, 3];
const clone = [...numbers];
const mergedArray = [...numbers, 4, 5, 6];
Enter fullscreen mode Exit fullscreen mode

7. Object Spread

Similar to array spread, object spread allows you to merge objects or add properties to an object easily.

// Longhand
const user = { name: 'John', age: 30 };
const updatedUser = Object.assign({}, user, { age: 31 });

// Shorthand
const user = { name: 'John', age: 30 };
const updatedUser = { ...user, age: 31 };
Enter fullscreen mode Exit fullscreen mode

Conclusion

By mastering these JavaScript shorthand techniques, you can significantly enhance your coding efficiency and produce more elegant and concise code. Destructuring assignments, spread syntax, and object shorthand will empower you to handle complex data structures and reduce redundant code. As you apply these shorthand techniques in your projects, you'll experience improved readability and increased productivity in your JavaScript development journey.

Happy coding !

Buy-me-a-coffee

Connect with me on Twitter, Linkedin and GitHub to stay updated and join the discussion!

Top comments (3)

Collapse
 
fleszar profile image
Fleszarjacek

Helpful

Collapse
 
abidullah786 profile image
ABIDULLAH786

I am glad you found it helpful.

Collapse
 
sujang959 profile image
Seonghun Cho

fyi cloning objects using the spread syntax does not clone nested objects. If you want to clone them too, use structuredClone().