4 ms·
Javascript is a typed language.
by ex_ex_nihilo 11y ago
Javascript is a typed language.
- alextgordon 11y agoand that type is the string. > 3 == "3" true > 3 * "3" 9
- ex_ex_nihilo 11y agoIn the above case, Javascript is coercing the strings to numbers. === does no type coercion. You can also coerce types manually. JS is not statically typed, but it is typed.
- alextgordon 11y agoI 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.
- Hupriene 11y agoSee step 10 and onward of: http://www.ecma-international.org/ecma-262/6.0/index.html#sec-addition-operator-plus 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.
- ex_ex_nihilo 11y agoYou 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
- rblatz 11y agoSorry, I meant statically typed, I always misuse that bit of terminology when talking about JavaScript, thanks for correcting me.