Some JavaScript Tips & Tricks
Today I’m gonna write a small article on some alternative way on few ways of writing JavaScript codes.
The old way to convert Number to String/String to Number
let num = 10;
let newNum = num.toString();
let str = '10';
let strNum = Number(num);
An alternative way to convert Number to String/String to Number
let num = 10;
let newNum = num + ""; //to String
let str = '10';
let strNum = +str; // to Number
Swap Values With Array Destructing
let a=1, b=2;
[a,b]=[b,a];
console.log(a); // 2
console.log(b); // 1
Remove Duplicates from Array
const arr = [1,3,5,2,1,5,5];
const uniqueValue = [...new Set(arr)];
console.log(uniqueValue); // [1, 3, 5, 2]
The alternative of .map() : An alternative of .map() is .from()
const person = [
{ name: 'Rakesh', age:13},
{ name: 'Abhishek', age:15},
{ name: 'Ritesh', age:18},
{ name: 'Ankur', age:17},
];
const displayPerson = Array.from(person, ({name})=> name);
console.log(displayPerson); Output : ["Rakesh", "Abhishek", "Ritesh", "Ankur"]
Hope this article can be of some help . Thanks !!