This content originally appeared on DEV Community and was authored by Jagroop
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
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
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")
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
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
That's all for this post, me with BTS come with another informative posts.
This content originally appeared on DEV Community and was authored by Jagroop
Jagroop | Sciencx (2022-05-07T11:27:19+00:00) JavaScript Tips and Tricks Part -II. Retrieved from https://www.scien.cx/2022/05/07/javascript-tips-and-tricks-part-ii/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.