6 Javascript Tips I Know !!

Abidurrahman
1 min readMay 3, 2021

--

src > https://pbs.twimg.com/profile_images/687353343195373569/vEP5wVGz_400x400.png

1 — undefined, null, 0, false, NaN, '' (empty string) are all falsy.

2 — Get a random item from an array

var items = [12, 548 , 'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' , 2145 , 119];

var randomItem = items[Math.floor(Math.random() * items.length)];

3 — Get a random number in a specific range

var numbersArray = [] , max = 100;

for( var i=1; numbersArray.push(i++) < max;); // numbers = [1,2,3 ... 100]

4 — Empty an array

var myArray = [12 , 222 , 1000 ];  
myArray.length = 0; // myArray will be equal to [].

5 — use === instead of ==

6 — use destructuring

The technique of breaking down the array elements and object properties as variables called, destructuring. Let us see it with few examples,let emojis = ['🔥', '⏲️', '🏆', '🍉'];let [fire, clock, , watermelon] = emojis;console.log(fire, clock, watermelon);Output:

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

--

--

Abidurrahman
Abidurrahman

Written by Abidurrahman

0 Followers

Full Stack Developer, React Developer

No responses yet

Write a response