Some JavaScript Tips & Tricks

Rakesh Kumar Shaw
1 min readAug 8, 2021

--

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 !!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Rakesh Kumar Shaw
Rakesh Kumar Shaw

No responses yet

Write a response