DEV Community

Pragmatic types: is JavaScript an untyped language?

stereobooster on August 23, 2018

I've found that some people call JavaScript a "dynamically, weakly typed" language, but some even say "untyped"? Which is it really? -- https:/...
Collapse
 
codevault profile image
Sergiu Mureşan • Edited

Really great coverage of types!

There are also some issues when checking for booleans since:

if (someObj.isValid) { ... }

Works fine, but depends what you really mean by this condition. If you are checking the actual value of someObj.isValid this will enter the if block only if it is true but if you are checking if someObj has the property isValid then this will return false when its value is either undefined or false so we would have to change our if condition:

if (someObj.isValid !== undefined) { ... }

This might be bad practice (correct me if that's the case), but for a beginner it is hard to understand why you need that. Especially if isValid is an array or string.

Collapse
 
stereobooster profile image
stereobooster

Good reminder. Type systems can protect against it. In terms of Flow it is called "sketchy" values. I need to write a small post about it.

Collapse
 
tux0r profile image
tux0r

Coercion is what is wrong with JavaScript's non-type system IMO. It is the source of too many problems, including security-related ones.

Collapse
 
shalvah profile image
Shalvah • Edited

Yeah, I think one of the greatest errors in the language's design was choosing to make invalid math operations return NaN rather than throw an exception. Nine times out of ten, programmers don't intend to multiply 3 by "five", so why not fail immediately and loudly?

Collapse
 
stereobooster profile image
stereobooster

Yes in JS coercion rules are pretty nasty

Collapse
 
stereobooster profile image
stereobooster

I wrote about untyped languages here