JavaScript tricks you didn’t know about

cover for JavaScript tricks you didn't know about

JavaScript is currently one of the most popular programming languages in the world.

In recent years it has seen rapid improvements with the popularization of front-end frameworks and the creation of NodeJS.

Today I’m going to share some of the lesser known Javascript tricks/features for the Javascript developers out there.

1- Double bang

We all know about the single bang (!), which is used to negate the value placed after it, for example:

Single bang(!)

What most people don’t know about is the double bang (!!). Logically one might think that it simply stands for two bangs, which pretty much do nothing to the value but that’s not how they work.

The double bang gives back the boolean representation of the value next to it, for example:

Double bang(!!)

2- Destructuring

This is one of my favorite JavaScript features! I wish every language had this feature.

Destructuring is actually a great name for what this is. Destructuring allows you to take all the contents of an array or object and place them inside another one by using ‘…’.

You can also destructure an object into multiple variables all at once like this

Image of destructuring an object
Destructuring object

or you can destructure two arrays into a new joint array like this:


Destructuring an array

3- Debugger

Console.log move over, a fancier king is in town.

The debugger keyword is great for when you feel like using console logs to debug your code, but you don’t really want to do that for all 10 variables you want to look at.

You can throw a debugger pretty much anywhere in your code and the browser will stop where you placed it.

At that point you can test anything that you have access to in that code block. Check the gif below:

Debugger

4- Console.warn and Console.error

The lesser known cousins to console.log().

The two allow you to highlight the output to the console.

They might be useful if you have a lot out logs but you want to make sure that you see those when they happen.

This is how the console.warn() looks like

Console.warn example

and this is how the console.error() looks like.

Console.error example

I am always learning about new development tricks as I go and I’ll make sure to share them with you as they come along.

What are some tricks and tips that you guys use?

Leave a Reply

Your email address will not be published. Required fields are marked *