Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I defer to Wikipedia's definition:

> A language is typed if the specification of _every_ operation defines types of data to which the operation is applicable, with the implication that it is not applicable to other types.

For a language to be typed, both the data must have a type and the operations must have a type specification.

    > "a" * "b"
    NaN
In a dynamically typed language, that specification is enforced at runtime.

    > true / false
    Infinity
In a statically typed language, that specification is enforced by a compiler.

    > setInterval(function(){ console.log("hi") }, "later")
    > hi
    > hi
    > hi
    > hi
    ...
As far as I can tell, JavaScript doesn't concern itself with any of that. It's not typed.


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


See step 10 and onward of: http://www.ecma-international.org/ecma-262/6.0/index.html#se... (for example)

Javascript is typed, it just makes a lot of crazy decisions about what operations an operator does.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: