DEV Community

7 Javascript Tips and Tricks

Ega Prasetya on September 26, 2020

Filter Unique Values The Set object type was introduced in ES6, and along with (...), the spread operator, we can use it to create a new...
Collapse
 
misobelica profile image
Mišo

Hi, while the conversion hacks are interesting, PLEASE note in bold (as with Math.*) that proper readable methods should be used. It's really cryptic in the real life code to decode these tricks because people think this is the only and proper way instead of these:

const isFalse = Boolean(0);
const numeric = String(29);
const integer = Number("36");  // or better parseInt("36", 10)
Collapse
 
egaprsty profile image
Ega Prasetya

nice corrections, thank you.

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Beware - truncating an integer and using any of Math.floor, Math.ceil, and Math.round are NOT the same thing:

console.log(-23.9 | 0); // Result: -23
console.log(Math.floor(-23.9)); // Result -24
console.log(Math.ceil(-23.9)); // Result -23
console.log(Math.round(-23.9)); // Result -24

console.log(23.9 | 0); // Result: 23
console.log(Math.floor(23.9)); // Result 23
console.log(Math.ceil(23.9)); // Result 24
console.log(Math.round(23.9)); // Result 24
Collapse
 
rtivital profile image
Vitaly Rtishchev

It's better to format json with spaces: JSON.stringify({}, null, 2)

Collapse
 
egaprsty profile image
Ega Prasetya

yea, it also good one.

Collapse
 
ditzdragos profile image
Dragos-Daniel Dit

Hi,
In the first example, the result includes an extra element: 4

Collapse
 
sirchris profile image
Krzysztof Florkowski

In the "Every and Some" paragraph more_random_numbers.some(isPositive) returns true 😉

Collapse
 
egaprsty profile image
Ega Prasetya

Thx for the correction bro

Collapse
 
bias profile image
Tobias Nickel

wow, i am using js for years, but the float to int is new, thanks man

Collapse
 
egaprsty profile image
Ega Prasetya • Edited

i hope this posting helpfull for u bro, thx!

Collapse
 
kasinathcr profile image
Kasinath Conjeevaram Ravi

There's a typo in Convert to Boolean code section. It should be:
console.log(typeof isTrue) instead of:
console.log(typeof true)

Collapse
 
egaprsty profile image
Ega Prasetya

Thx for correction bro