DEV Community

Cover image for My Favorite JavaScript Tips and Tricks

My Favorite JavaScript Tips and Tricks

Tapas Adhikary on September 08, 2020

Motivation Most of the programming languages are open enough to allow programmers to do things multiple ways for a similar outcome. Java...
Collapse
 
juliobetta profile image
Julio Betta

hey, awesome article. just a small correction

let age = person.age ?? 35; // sets the value 35 if age is undefined, 0, null, '' etc

because of the null coalescing, if person.age === 0, the variable age is 0.

const person = {
  name: 'Adhikary',
  age: 0
};

const age1 = person.age ?? 35; // => 0
const age2 = person.age || 35; // => 35
Collapse
 
atapas profile image
Tapas Adhikary

Big thank for helping me to correct the typo.

Collapse
 
elliot profile image
Elliot

Hadn't seen the isRequired idea before! Clever list :)

Collapse
 
atapas profile image
Tapas Adhikary

Thanks Elliot!

Collapse
 
hnicolas profile image
Nicolas Hervé

Number.isInteger method was designed to be used with numbers in order to test if it is an integer or not (NaN, Infinity, float...).

Number.isInteger(12) // true
Number.isInteger(12.0) // true
Number.isInteger(12.5) // false

developer.mozilla.org/en-US/docs/W...
Using typeof mynum === "number" is a better solution to test if a value is a number.

typeof mynum // number
typeof mynumStr // string
Collapse
 
jankapunkt profile image
Jan Küster • Edited

Note, that the Number rounding of floats in JavaScript is not accurate

(0.1 + 0.2) === 0.3 // false

Since this rounding affects integer representation, too, you should also consider Number.isSafeInteger

developer.mozilla.org/en-US/docs/W...

Also please consider Integer boundaries in JS, that are represented by Number.MAX_SAFE_INTEGER and Number.MIN_SAFE_INTEGER

developer.mozilla.org/en-US/docs/W...

developer.mozilla.org/en-US/docs/W...

Edit: sry wanted to post on top level, somehow ended up as comment on yours

Collapse
 
atapas profile image
Tapas Adhikary

Awesome, thanks Jan!

Collapse
 
ocalde profile image
Oscar Calderon

I thought I would know everything in this list, but learned something new!!! Didn't know about the nullish coalescing operator, pretty useful! Thank you Tapas

Collapse
 
atapas profile image
Tapas Adhikary

Thanks, Oscar!

Collapse
 
marcellothearcane profile image
marcellothearcane

It's quite new, so it won't work everywhere yet.

Collapse
 
rajmohanpdy profile image
rajmohan s

?? operator not working for me. shows as below
SyntaxError: Unexpected token '?'

Collapse
 
atapas profile image
Tapas Adhikary

Which browser are you using? It should work.

let person = {name: 'Jack'};
let age = person.age ?? 35; // sets the value 0 if age 0, 35 in case of undefined and null
console.log(`Age of ${person.name} is ${age}`);
Enter fullscreen mode Exit fullscreen mode

Output, Age of Jack is 35

Also, check if there is a syntax error. You can paste the code as a comment.

Collapse
 
klvenky profile image
Venkatesh KL

Points 3,8,9 blew my mind away. Especially the user of a function to throw errors 🙏👏👏

Collapse
 
atapas profile image
Tapas Adhikary

Great.. Thanks, Venkatesh!

Collapse
 
sakar_dhana profile image
Sakar

Really nice tricks to know. Nice work keep this good work going...
Good health

Collapse
 
atapas profile image
Tapas Adhikary

Thanks Sakar for the encouragements!

Collapse
 
olsard profile image
olsard

Awesome, thanks for sharing!

Collapse
 
atapas profile image
Tapas Adhikary

Thanks for reading and commenting!

Collapse
 
fahad07_khan profile image
Fahad Khan

Good tricks shared in this article I learnt some new also

Collapse
 
atapas profile image
Tapas Adhikary

Thanks Fahad!

Collapse
 
kingleo10 profile image
Niroj Dahal

I have used window.location.hash in pages where tab content need to be loaded initially based on url hash. I find this pretty useful

Collapse
 
atapas profile image
Tapas Adhikary

Cool, thanks for sharing Niroj!