DEV Community

Discussion on: Avoiding Weird Javascript Behaviour (true + true === 2 but true !== 1)

Collapse
 
renoirb profile image
Renoir • Edited
(true + true === 2)

Happens because of type coercion occuring right at "+ true" and also because there is more than one equal (not an assignement).

In the same way, if you need a date epoch interger, instead of using

new Date().getMilliseconds()

you can just do

+new Date()

Plus at the beginning of the statement forces type coercion into number.

Dr. Axel Rauschmayer's book Speaking JS is amazing and full of well written explanations. Including what I'm outlining here.

Collapse
 
neutrino2211 profile image
Mainasara Al-amin Tsowa

Thanks, going to fix that