JavaScript Tips and Tricks Part -II

Jagroop Singh - May 7 '22 - - Dev Community

Hi @all,

I have come back with a second part of JavaScript Tips and Tricks.
You can see the first part here :
JavaScript Tips and Tricks!

So, let's go with Jin 🧡 :

Top Tips and Tricks :

Check if Property Exist in Object: With the help of in keyword in JavaScript, we can check that whether a property existed in JavaScript object or not. For Example:

const BTS = { name: 'Kim Seok-jin', stageName : 'Jin'};
console.log('name' in BTS); 
console.log('age' in BTS); 

OUTPUT :
true
false
Enter fullscreen mode Exit fullscreen mode

Easy Exchange Variable : During the initial period of learning programming, everyone went through swapping numbers using temp variable or using some mathematical calculation. But one can do this easily in JavaScript using destructuring.** For example :**

var a = 12;
var b = 6;
[a,b] = [b,a]
console.log(a,b) ;

OUTPUT :
6 12
Enter fullscreen mode Exit fullscreen mode

Shorten the Console log : If you're fed up with typing console.log() to check data or for debugging, then don't worry, I will share my personal shortcut way to shorten the console log. Here it is :

var BTS = console.log.bind(document)
BTS("JIN's  Epiphany song is the best")
Enter fullscreen mode Exit fullscreen mode

Convert Number to Binary If you want to convert a number into binary number then no need to use or remember mathematical formula, I will be sharing a simple trick as :

var num = 2000;
console.log(num.toString(2));

OUTPUT :
11111010000
Enter fullscreen mode Exit fullscreen mode

Remove duplicates from arrays using Set : It is a very simple yet effective method of removing duplicates from arrays using a simple one-liner.

var BTS =['J-Hope','Jin' ,'JK', 'RM' ,'J-Hope' ,'JK' ,'V' , 'Jimin' ,'V','Jin' ,'Suga' ,'Suga','Jimin']

console.log(...new Set(BTS));

OUTPUT :
J-Hope Jin JK RM V Jimin Suga
Enter fullscreen mode Exit fullscreen mode

That's all for this post, me with BTS come with another informative posts.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player