DEV Community

Cover image for 5 neat JavaScript tips

5 neat JavaScript tips

Dalibor Belic on March 21, 2021

In this post, I'll show you 5 neat JavaScript tips that will help you become a better developer. Although this post requires some knowledge of Java...
Collapse
 
kazppa profile image
Kazppa

May i ask some explanation about the "benefits" of destructuring ?

Collapse
 
akashkava profile image
Akash Kava

Destructuring improves speed by accessing members by one time assignment. Every a.b member expression is slower as it travels prototype chain for access.

The argument becomes immutable, reassigning destructred variable will not modify the source.

Collapse
 
avatarkaleb profile image
Kaleb M

I don't think this would be true if the destructured key was an object, it would still be by reference.

Overall I agree that destructuring is cleaner though!

Thread Thread
 
akashkava profile image
Akash Kava
const user = { name: "Akash" };
let { name } = user;
name = undefined;
console.log(user.name);
Enter fullscreen mode Exit fullscreen mode

Should it print undefined or "Akash"?

In no way destructured variable is by reference. Reassigning destructured variable is immutable (not changing property of object held in destructured variable if it is an object).

Thread Thread
 
avatarkaleb profile image
Kaleb M

Yes, you assigned the string, so no not by reference. If name itself was an object, it would be by reference yea?

Thread Thread
 
akashkava profile image
Akash Kava

Try it and show me a working example.

Thread Thread
 
qm3ster profile image
Mihail Malo
const user = { name: { first: "Akash", second: "Kava" } }
const { name } = user
name.first = "🅱️epis"
console.table(user.name)
Enter fullscreen mode Exit fullscreen mode

Sir, please, there is no need to be rude.
(and yes, you will say to do const { name: { first } } = user, but pls. Pls. Sir, pls.)

Thread Thread
 
akashkava profile image
Akash Kava

In which way the word "Reassigning destructured variable" is similar to name.first = ... ? The whole purpose of destructuring is not to use member expression name.first. And yes let { name: { first } } = user is the correct way to use desctructering. user is immutable here. It is important to note that we can pass an object to a function which will not change the original object if function is written correctly using destructering. JavaScript compiler can also optimize it by knowing that the function is pure.

Thread Thread
 
vkbr profile image
Vivek B

Destructuring in itself doesn't make it immutable. In the example @akashkava where you re-assigned value to "name" variable and it didn't affect the original object is because the variable of type String (or any other primitives) are immutable and not because it is coming from a destructured object.
Your example is same as

let name = user.name; // string
Enter fullscreen mode Exit fullscreen mode

which makes it immutable because of the data type. @qm3ster 's example showed that destructured values could be mutated which will mutate the origin object.

Destructuring is just syntactic sugar to provide more readability.
const name = user.name is same as const { name } = user

Collapse
 
daliboru profile image
Dalibor Belic

Sure.
I remember once a senior dev told me (I was a junior) that it's not just enough to do something. You have to do it the right way. Destructuring makes our code cleaner and more readable.

Collapse
 
matgott profile image
matgott

Destructuring isn't working "with reference". This could be confusing.

Objects in Javascript are mutables, so if you are destructuring an object which one of it properties is another object, the variable created for that property will point to the same object in the Heap.

Collapse
 
blaketweeted profile image
Blake Campbell

I had no idea you could style console.logs, that's amazing!

Collapse
 
daliboru profile image
Dalibor Belic

Yep, pretty neat! :)

 
morgboer profile image
Riaan Pietersen

In the days of ActionScript we were able to use a "with" statement.. kirupa.com/developer/actionscript/...

Is there something similar in JS?! I wonder.. :)

Collapse
 
pris_stratton profile image
pris stratton

The callback into Promise one is so handy. It means you can use async await as well which is so much easier to write.

Collapse
 
mafee6 profile image
MAFEE7

Thx For the tip!

Collapse
 
daliboru profile image
Dalibor Belic

Welcome!

Collapse
 
codenamecookie profile image
CodenameCookie

There is a typo. You wrote 'trase' instead of 'trace'. Good article though.

Collapse
 
daliboru profile image
Dalibor Belic

Tnx!

Collapse
 
jportella93 profile image
Jon Portella

FYI there's a Node.js util that does the promisifying for you, you don't need to write your own. Thanks for sharing!

Collapse
 
qm3ster profile image
Mihail Malo

And a lot of nodejs builtins come in proper promise versions now, not by making you promisify them in userspace!

require("fs/promises").readdir('.').then(console.log)
Enter fullscreen mode Exit fullscreen mode

Hurrah!

Collapse
 
benjaminvanryseghem profile image
Benjamin Van Ryseghem

obj.key is undefined, isn't it ?

Collapse
 
alitahashakir profile image
Ali Taha Shakir

What if I have 15-20 elements which I need to show in component, is it good / good practice to destructure 20 elements as arguments?

Collapse
 
daliboru profile image
Dalibor Belic

Yes! In that case, I would do the 2nd version: function (tiger) { ... and then inside the function: { endangered, latin, ... } = tiger