Alternative approaches in JavaScript
It's a short article demonstrating alternative ways of writing some of the tradition JavaScript codes.
The old of converting Number-Strings
let num = 10;
let newNum = num.toString();
let str = "10"
let strNum = Number(str)
The alternative way for converting Number-Strings
let num = 10;
let newNum = num + "" // converts to String
let str = "10";
let strNum = +str // converts to Number
Swap two values
let a = 1, b = 2;
let t = a;
a = b;
b = a;
Alternate way for swapping two values using Destructuring
let a = 1, b = 2;
[b,a] = [a,b]
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 !!