Learn the nuts and bolts of Javascript & WebDev

One tip at a time

From developer to developer

 

Every few days, you encounter and learn something new. It's called experience, and the more of it you have the better you are

 

I'm no exception, it happens to me too. These mails are the Aha! moments I encounter almost on a daily basis.

 

Tap into my experience and improve faster as a professional.

A weekly newsletter of tips & tricks

 

Short emails, designed to be read 10 minutes tops

 

Mainstream technologies, like Vanilla JS, Lodash, RxJs, and React, that you're likely to use on a day-to-day basis

 

Based on my experiences with SPAs and front-end best practices

A 100 tips challenge

 

Earlier this year, I've made a challenge for myself of writing a list of 100 tips that are useful and interesting

 

To my surprise, I've finished in around a week. I realized that it contained many tips that are far from trivial, but an elegant solution to an everyday problem.

 

In this newsletter, I'm posting them one-by-one, in a weekly manner

A sneak peek

 

To get a feeling what it is all about, here is the 1st tip. Enjoy!

 

Swap variables using the array destructuring operator

Swapping two variables is a typical 0th round technical interview question. It is specifically aimed to immediately disqualify people who have never written code in an imperative language.

The textbook example is, of course, to use a third variable:

var tmp = a;
a = b;
b = tmp;

There are a few unusual and in fact, ingenious solutions you can use if the variables are numbers, like the bitwise XOR method:

a = a ^ b;
b = a ^ b;
a = a ^ b;

But ES6 brought the array destructuring operator, which provides the most readable version of all:

[b, a] = [a, b];

This solution constructs an array with a and b, in this order. Then destructures it in reverse order, so b gets the first value, which is a's, and a gets the second value, which is b's.

How fast is it? Compared to the other methods, it's slow. It constructs an array, assigns the variables, and it leaves some garbage which has to be collected later. But in absolute terms, it's fast, and you shouldn't worry about it.

Now you can ace the 0th step of the technical interview!

Try it
References

Privacy policy

 

You can unsubscribe at any time, in which case you won't receive emails anymore

 

No spam, no selling of your email address in any ways

 

No sharing of your email address with a third party, except for providers needed to run the service

Learn something each week

Zero spam of course, unsubscribe any time.