loading...

7 Javascript Tips and Tricks

ga profile image Ega Prasetya ・2 min read

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!

Posted on by:

ga profile

Ega Prasetya

@ga

No matter what i`m learn, kill with documentation.

Discussion

pic
Editor guide
 

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)
 

nice corrections, thank you.

 

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
 

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

 

yea, it also good one.

 

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

 

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

 

Thx for the correction bro

 

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

 

i hope this posting helpfull for u bro, thx!

 

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

 

Thx for correction bro