Hi,
could anyone explain or send a link on this topic?
return a.match(/\d/) - b.match(/\d/); // ["1"] - ["2"] returns -1
How does this work? I would have never guessed you can do this with one element arrays.
Hi,
could anyone explain or send a link on this topic?
return a.match(/\d/) - b.match(/\d/); // ["1"] - ["2"] returns -1
How does this work? I would have never guessed you can do this with one element arrays.
the topic is type coercion
-
is a number operator, so if you are using it with stuff that is not numbers, it tries to coerce those to numbers. If it can’t, it returns NaN
(not a number)
this is possible because javascript is a loosely typed language, where it is easier to pass from one data type to an other.
type coercion can give a lot of unexpected behaviour, and is also the reason for which true + true === 2