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.
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.
Convert to Boolean
Besides the regular boolean values true and false, Javascript also treats all other values as either 'truthy' or 'falsy'.
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.
Convert to Number
The opposite can be quickly achieved using the addition operator +
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.
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.
Happy Coding!
Top comments (12)
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:nice corrections, thank you.
Beware - truncating an integer and using any of
Math.floor
,Math.ceil
, andMath.round
are NOT the same thing: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)
returnstrue
😉Thx for the correction bro
wow, i am using js for years, but the
float to int
is new, thanks mani 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