Top JavaScript Tricks and Tips
Tags: javascript
Published: 2021-08-10 05:00:31

<h1>Swap elements of an array</h1>


Lots of array operations require the swapping of elements, for example, sorting. You would need to write a small utility function and The typical algorithm requires a temp variable for swapping. But ever since, I have learned this trick. It’s awe-inspiring. During a couple of coding interviews, I used this trick to impress the interviewer, and it has worked well. Some of them go, What is that syntax?

We will use destructing to achieve that. Destructing was introduced in the ES6 version of JavaScript.


You must be wondering how does it work without using a temp variable? Destructuring holds value for you, so you don’t need that additional temp value. You can also use destructuring to swap two variables.

While we are on the subject of swapping variables, let me show you another neat trick. I was once asked this question during an interview. The interviewer asked me to swap number variable values without using temp variables or destructuring. I ended up using math to achieve a three-step process, but recently I simplified it into a single-line solution. Though this is a neat trick, please don’t use this in your project as it may confuse the reader. You can, however, use it as an interview question if you want this to be a painful experience for them :).

<h1>Destructing Alias</h1>

You have an object from which you want to create a variable from one of the properties. You can easily do it. But often, you want to use a variable name that is different than the property name. For example, if you had a property called “age,” the variable name you want to create is “profileAge.” you would need to create an alias. You can easily do it in multiple lines, but this is a neat solution in a single code line.

<h1>Shuffling</h1>

Many algorithms require shuffling elements of an array. During a coding interview, you may get a question to build a game. And most games involves some shuffling algorithm. Once I interviewed at Uber, they asked me to make a minesweeper game, whereas as a first step, I had to build a layout with randomly placed mines. If you need to develop a shuffling algorithm, it requires a bit of thinking; recently, I stumbled upon this little trick that makes a shuffling piece of cake.

To understand the above code, you need to know how the sort method works on arrays. Sort takes a callback function; usually, based on the arguments of the arrays, we can decide where we want to place the next element. This function can return positive, negative, or 0 values. We can randomly create these values using a random number generator, which is always positive, so subtracting 0.5 would give us about a 50% chance of returning a positive or negative value.

<h1>Type conversion</h1>

There are many different ways to convert one type to another in JavaScript. Some look cleaner than the others, so make sure you don’t compromise the readability of the code by using a particular way.
1. to String
2. to Number
3. Float to Integer
4. to Boolean

Positive Integer to binary or hexadecimal or octal

<h1>Remove duplicates from an array</h1>

This one-line solution for removing duplicate values from the Array is neat. JavaScript introduced a new data type called `Set` in the ES6 version. Sets, by default, don’t allow duplicated values. So this simple trick involves converting an Array to a Set, which removes the duplicate values and then converting the Set back to an Array.