Member-only story
Introduction
There are two things in JavaScript that I love using for cleaner code. And if you read the title, you can guess that those are destructors and includes function. In this post, I will give you a short overview of them and the use case where I use them.
Destructors
What is destructuring? Destructuring is the assignment syntax where it is possible to unpack values of arrays and objects. It is still not fully clear? Maybe it will be better with an example.
Let’s take an example of the following simple object containing two properties:
const person = {
firstName: “John”,
lastName: “Doe”
}
If we wanted to unpack properties in the old way, we would use dot or square brackets.
const firstName = person.firstName;
const lastName = person[“lastName”];
With the destructors, there is no need for either. We use curly brackets on the left side and names of the properties and the source object on right to unpack them.
const { firstName, lastName } = person;