javascript - Empty object minus/plus empty object, and reverse value -
i have question. in console i'm training "arithmetics", like:
false + true // 1
so, question is, why:
[] - {} // nan
and
{} - [] // -0
can explain this, because both of them types object. , know javascript have , falsy values. so, if take
boolean({}) // true boolean([]) // true
in both have true
, think result like:
true + true // 2
or
true - true // 0
in context (in console), {} empty block nothing, result same
- []
and [] coerce 0, seen by:
+[]
if want {} treated empty object, try this:
({}) - []
you nan now, since empty object not coerce number.
update: work should expect in console (returning nan):
({} - [])
Comments
Post a Comment