Hacker News new | ask | show | jobs
by ex_ex_nihilo 3897 days ago
You are a bit confused. The == operator specifically does implicit type conversion. You want to use === to avoid implicit coercion. And you always have the option to explicitly coerce type, as I explain below.

Javascript's type coercion leads to some wacky things like

> [] == []

> false

But it's actually all perfectly consistent once you understand what the compiler is doing with type (and JavaScript is compiled, not interpreted). == is an operator that will always try to coerce its operands to numbers, but the way it gets there can be circuitous.

You can specify type in JavaScript and avoid implicit coercion, you can use type constructors (generally worse) or certain unary operators. Ex:

> var foo = "12"

> var bar = +foo - 5; //+ explicitly converts foo to a number