DEV Community

Ega Prasetya
Ega Prasetya

Posted on

7 Javascript Tips and Tricks

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 array with only the unique values.

Alt Text

Every and Some

The every function returns a boolean. If all elements in the array pass the test, true will be returned. The some function tests whether at least one element in the array passes the test implemented.

Alt Text

Convert to Boolean

Besides the regular boolean values true and false, Javascript also treats all other values as either 'truthy' or 'falsy'.

Alt Text

Convert to String

The quickly convert a number to a string, we can use the concatenation operator + followed by an empty set of quatation marks.

Alt Text

Convert to Number

The opposite can be quickly achieved using the addition operator +

Alt Text

Quick Float to Integer

if you want to convert a float to an integer, you can use Math.floor(), Math.ceil(), or Math.round(). But there is also faster way to truncate a float to an integer using |, the bitwise OR operator.

Alt Text

Format JSON Code

the stringify() method takes two optional parameters: a replacer function, which you can use to filter the JSON that is displayed, and a space value.
The space value takes an interger for the number of spaces you want or a string (such as '\t' to insert tabs), and it can make it a lot easier to read fetched JSON data.

Alt Text

Happy Coding!

Top comments (12)

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