DEV Community

Cover image for JavaScript is not an untyped language

JavaScript is not an untyped language

MiguelMJ on June 24, 2022

JavaScript's popularity At the time of writing this post (Jun 2022), JavaScript is #3 in the PYPL index, only beaten by Java and Python ...
Collapse
 
mistval profile image
Randall

JS has types and the simplest proof is the existence of the typeof operator. Still, if someone says JS is untyped, I'll know what they mean and won't do a "well akshually ..." because it's unproductive and pedantic.

Collapse
 
peerreynders profile image
peerreynders • Edited

because it's unproductive and pedantic

You have to be careful. Being too accepting of "you know what I mean" can easily cascade over time into communication that is so ambiguous that it is useless.

Like it or not, as software developers we have to communicate effectively with our colleagues and machines. With machines for the most part we stare at a syntax error or unit test run time error (if we're lucky) when we are imprecise or plain wrong.

The presumably self-correcting nature of communication among colleagues is at best unreliable. Too many times I've seen two parties agreeing with one another simply because they were hearing what they wanted to hear, not what was actually being said.

This is why for example in DDD ubiquitous language is so important.

So the trick is to correct in the service of keeping communication as accurate as possible without being pedantic about it and being tolerant of the fact that at times we all make mistakes.

Just yesterday in the context of JSX I was talking about markup 🤦.

Collapse
 
mistval profile image
Randall

There's no avoiding misunderstandings, they can happen regardless of how precise your language is. There are times when correcting someone can be beneficial, and other times when you're just telling them things they already know, not doing anyone any good, and looking pedantic. Ultimately, that's a judgement call based on the unique circumstances of the situation. If someone says "JS doesn't have types", I think it's extremely likely that they are in fact aware that JS has strings, numbers, null, and undefined. There may be times when launching into a detailed explanation about it is helpful, but most of the time it's probably not.

Thread Thread
 
peerreynders profile image
peerreynders

I think there's also cultural issue; "correction" as a means asserting superiority but I think we need to move beyond that.

And perhaps in lieu of a "detailed explanation" sometimes it may make more sense to ask some questions in order to establish whether certain assumptions are actually shared.

Collapse
 
miguelmj profile image
MiguelMJ

You're right. Just notice that my problem arises when some people are telling beginners that there are no type errors in JavaScript and that's a good thing.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

Types are more than just "you can't multiply text with a number". Javascript has exactly one type: "any".


EDIT: I also want to point out that JS could have no casting whatsoever and it would still be an untyped language. Say, for example, you have this pseudo-code in some JS-like language:

function foo(value) {
   print(value + 1)
}
Enter fullscreen mode Exit fullscreen mode

Say addition is only defined on numbers and you can only print text. Without any conversion, that would result in a runtime error for any imaginable input value. But can you point out the type of value in this code? That's why it's untyped.

Collapse
 
mvasilkov profile image
Mark Vasilkov

that would result in a runtime error

Any error in JS is a runtime error, naturally.

What you're saying is that the only way a language can be typed is if it's a compiled language with all of the type info known at compile time.

Which leaves you with like... three programming languages or so.

You can trivially write code similar to your example in both C and Java, with the same runtime error. Not because they're untyped, but because their type system is unsound, which is, to me, a wholly different can of worms.

Collapse
 
miguelmj profile image
MiguelMJ

Could you elaborate? What else are types supposed to be? As far as I understand, types are semantic information attached to values (all values, not only variables) that determine in what context they can be used.
If you mean that any variable can have any type... wouldn't that be precisely talking about just a little part of what types are?
Btw, I'm sure you know in JS you can multiply text and numbers (results in NaN, but you can).

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

As far as I understand, types are semantic information attached to values

No. At least not in the strict sense. Types are syntactic information attached to expressions.

In the less strict sense, types are... yes. All languages general purpose are necessarily "typed" because you just can't get any work done if you can only ever support one kind of value. But that's not what people talk about when they say a language is typed or untyped, because it'd be a pointless distinction.


Btw, I'm sure you know in JS you can multiply text and numbers (results in NaN, but you can).

Yes, as I mentioned, that was a hypothetical JS-like language to illustrate my point (print isn't a thing in JS either)

Thread Thread
 
miguelmj profile image
MiguelMJ • Edited

I think the strict sense would be interpreting types as semantic information. Expressions can't have a type attached as syntactic information precisely because of polymorphism. Something that's present even in statically typed languages.

Consider C++ for example, that has multiple inheritance and where inheritance behaves as subtyping. You might know at least one type of a value thanks to it being statically typed, but if a you can have an undefined number of types attached to a value at any time, how can you say that the other types are syntactic information? To say that those types are not relevant... is to talk in a less strict sense.

void foo(AbstractParentClass value){
    value.bar(); // value has more than one type, here we only know one
}
Enter fullscreen mode Exit fullscreen mode

A pointless distinction... depends on the context too. There are languages without type systems, that may or not be general purpose, but that still need to be called "untyped" and don't fall in the same category as JS.

Thread Thread
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

Expressions can't have a type attached

It seems you're fundamentally misunderstanding what types are, because being attached to expressions is exactly what they're all about. You can compute the type of any expression at compile-time simply by looking at its components.

If you have an expression f(a) and you know that a : A and f : A -> B then you know that the result type of that expression is B.

Javascript does this too, in a sense, except that there's only one type, so any program is valid by default.

C++ is a rather bad example because the inheritance complicates things quite a bit. Being a good object-oriented language comes with the side-effect of making it a not-so-good typed language.

Collapse
 
bytebodger profile image
Adam Nathaniel Davis • Edited

I'm sorry, but this feels totally like a strawman argument. Whoever said that JavaScript is "untyped"? Whoever said that ANY language is "untyped"?? In fact, what in the heck are you even referring to when you say "untyped"??? I've never heard of such a thing. There are strongly-typed languages. And there are dynamically typed languages. But every single language I've ever heard of has types.

Collapse
 
miguelmj profile image
MiguelMJ

Please read the whole post and other comments. Thanks for your comment.

Collapse
 
bytebodger profile image
Adam Nathaniel Davis

While I really appreciate the condescending reply, the simple fact is that I did read the whole post. And I don't need to read all of the comments before I supply my own comment.

Thread Thread
 
miguelmj profile image
MiguelMJ • Edited

I'm sorry, Adam, some answers to your questions are in the post, so I thought you didn't read it. Answering your questions:
Lots of people classify Javascript as untyped, from people in this comment section to people in the linked SO discussion and Brendan Eich himself (which I mention in the post).
Lot of computer scientists have talked about untyped languages, that don't have a type system, and some examples are found in formal languages like Turing machines, to some shell languages, assembly languages, esoteric languages or pure logic languages, where the absence of type restrictions makes them fall in the untyped category.
Strong, weak, static and dynamic are terms applicable to languages with a type system, so when a language just lacks one, they don't apply.
I hope to have answered your questions.
About the strawman, I guess I just had bad luck to stumble upon people that you have not, saying things that made me write this. No need to invalidate my point.

Thread Thread
 
ghostclonelol2000 profile image
<}:-{~ .A.K.a. DOOM • Edited

"Mmmmmmmmmmmm"

herge

Collapse
 
lexlohr profile image
Alex Lohr

JavaScript has types, but instead of trying to make type safety a priority and throw TypeErrors every time when they don't add up (and not only if you try to use them in cases when they cannot be coerced), it aims for robustness. If you know the rules of type coercion, you can make them work for you.

Collapse
 
miguelmj profile image
MiguelMJ

Exactly. That's part of what I mean to say.
Thanks for your comment!

Collapse
 
peerreynders profile image
peerreynders • Edited

If there's something new programmers hate is error messages. I guess that's why I've seen listed as one of its advantages over other languages that JavaScript produces no type errors because it's an untyped language.

This may be useful for future reference:

"Scheme is dynamically typed. The lack of a type system means that we don’t have to spend energy on finding and explaining type errors with the same care with which we explain syntax errors. Better yet, when we use Scheme to teach design principles we can informally superimpose a type system and use the types for program design."

The Structure and Interpretation of the Computer Science Curriculum (2004)

i.e. the actual message is that dynamically typed languages are typically designed with a certain level of "type leniency" (for resiliency) which reduces the number of type errors the learner is faced with.

That's very different from "no type errors".


PS: It's interesting that it is a commonly held belief that statically typed languages are better for beginners given the additional guardrails that static type analysis can provide.

However it seems that dynamically typed languages may be better because REPL-driven programming provides a much more fluid feedback loop than the usual edit-compile-run loop can.

Collapse
 
miguelmj profile image
MiguelMJ

Thanks for your comment and support in general, @peerreynders. I've bookmarked several links you already shared, it's useful material.
Thanks again and sorry for the late reponse!

Collapse
 
boilertom90 profile image
Tom Hoffman

If that’s your definition of a typed language, ok. I’ll take typescript any day….

Collapse
 
miguelmj profile image
MiguelMJ

That's the definition you'll find in PLT books and papers. TypeScript is definitely worth taking, sounds great.
Thanks for your comment!

Collapse
 
bias profile image
Tobias Nickel

I want to add, that js in fact has many typesystems.

  • api types: graphql and grpc.
  • compiletine types: from typescript
  • database schemas: mongoose, typeorm, sequelize
  • typevalidation: json-schema, Joi, super-struct...
Collapse
 
miguelmj profile image
MiguelMJ

Although I'm talking about vanilla JS, I guess you could talk about extended versions of the language, using other frameworks and utilities.
Thanks for commenting!

Collapse
 
fyodorio profile image
Fyodor

Such a nice and interesting conversation here in the comments, thanks for provoking that @miguelmj 👍

Collapse
 
miguelmj profile image
MiguelMJ

Thank you for reading! ❤️

Collapse
 
jcubic profile image
Jakub T. Jankiewicz • Edited

I don't understand why to write this post. AFAIK there are no untyped languages. There are no such things. Every language has types. Maybe except some esoteric languages like BrainF*ck. They can be static or dynamic but they're always there.

Collapse
 
peerreynders profile image
peerreynders

Primarily because there's a certain sloppiness in the current discourse.

I've seen people repeatedly talk about TypeScript being "strongly typed" when they (as you have correctly observed) should state "statically typed".

Erlang for example is a strongly typed language but it's also dynamic.

It's important for professionals to use standard terminology, and like it or not,

This is what the article is all about.

Lets get the terminology straight so that we can all communicate in a more precise and effective way.

Collapse
 
miguelmj profile image
MiguelMJ • Edited

Please, read the whole post.
About untyped languages: some shell scripting languages are untyped (type-like restrictions come from subprocesses and not from a native type system), some esoteric languages are untyped, assembly languages are untyped, pure Prolog is untyped, Turing machines are untyped. There might be other examples I don't know personally.
Thanks for your comment.

Collapse
 
peerreynders profile image
peerreynders

Is there an actual accepted definition of 'untyped' somewhere or is it one of those terms that only carry meaning within a very specific context?

Thread Thread
 
miguelmj profile image
MiguelMJ

My main references for the subject are Programming Language Theory textbooks written by L. Cardelli or R. Harper. They and other authors (I would need to check again the bibliography for the names) use 'untyped' to refer to languages where values have no types, or a single type, and therefore don't apply type restrictions of any kind.

Thread Thread
 
jcubic profile image
Jakub T. Jankiewicz

I only know one formal name "untyped lambda calculus". But only because there is typed lambda calculus. So obviously normal lambda calculus is untyped.

Collapse
 
kvetoslavnovak profile image
kvetoslavnovak • Edited

Were did you get the definition of Javascript to be an untyped language?
I believe the official definition is it has dynamic types (is weakly typed).

Collapse
 
miguelmj profile image
MiguelMJ

Please read the whole post. Thanks for your comment.