115

I've found that some people call JavaScript a "dynamically, weakly typed" language, but some even say "untyped"? Which is it really?

Deniz Dogan
  • 25,711
  • 35
  • 110
  • 162

9 Answers9

142

JavaScript is untyped:


(source: no.gd)

Even Brendan Eich says so. On Twitter, he replied to a thread that linked to this question:

... academic types use "untyped" to mean "no static types"...

So the problem is that there's a few different definitions of untyped.

One definition has been talked about in one of the above answers - the runtime doesn't tag values and just treats each value as bits. JavaScript does tag values and has different behaviour based on those tags. So JavaScript obviously doesn't fit this category.

The other definition is from Programming Language Theory (the academic thing that Brendan is referring to). In this domain, untyped just means everything belongs to a single type.

Why? Because a language will only generate a program when it can prove that the types align (a.k.a. the Curry-Howard correspondence; types are theorems, programs are proofs). This means in an untyped language:

  1. A program is always generated
  2. Therefore types always match up
  3. Therefore there must only be one type

In contrast to a typed language:

  1. A program might not be generated
  2. Because types might not match up
  3. Because a program can contain multiple types

So there you go, in PLT, untyped just means dynamically typed and typed just means statically typed. JavaScript is definitely untyped in this category.

See also:

Community
  • 1
  • 1
Brian McKenna
  • 45,528
  • 6
  • 61
  • 60
  • 7
    And of course, "no static types" is not the same as "no type declarations" either. – Andreas Rossberg Feb 06 '12 at 20:26
  • +1. You should perhaps also link [this](http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html) in your answer. This "weakly typed" bs is too widespread. – missingfaktor May 15 '12 at 11:59
  • +100,000,000 - welcome the minority of programmers that actually understand not only 1 language, but its differences and relationships with other languages. Now watch in horror as the majority abuses and twists everything...like andrew hare's answer on this page... – Jimbo Jonny Oct 19 '12 at 16:59
  • Proving my own point, I just had an answer deleted by a mod for answering another question similarly. Guess I should have included the mozilla graphic. – Jimbo Jonny Oct 19 '12 at 18:16
  • @Gho5t Crockford is wrong. He is confused about quite a few things. Types and monads are examples. – Brian McKenna Mar 01 '13 at 16:51
  • Crockford insists on K&R bracing. I mean, seriously. WTF. (Otherwise, I find he has a lot of interesting things to say about programming). Oh, and I already know about the *one* use case issue with semicolon insertion. – Almo May 30 '14 at 20:12
  • 3
    Programming Language Theory is correct. The giant screenshot is incorrect. – Alex W Jun 11 '14 at 14:55
  • 2
    I was glad to see that this answer referenced [Harper's blog post](https://existentialtype.wordpress.com/2011/03/19/dynamic-languages-are-static-languages/) but it would be nice if the PLT community dropped "untyped" in favor of "unityped". The definition of "untyped" meaning "just bits" actually makes sense. To use "untyped" to describe a language whose formal specification uses the word "type" and says that it has seven types (Undefined, Null, Number, String, Boolean, Symbol, and Object) is really confusing. Most people do not want to distinguish this notion of type from the PLT def. – Ray Toal Jun 15 '15 at 01:07
  • 5
    Your `1. 2. 3.` for an untyped language doesn't match JavaScript. There is not *only* one type -- there are about 4 - 5. So how does that demonstrate JavaScript is untyped? – Don Cheadle Jul 24 '15 at 19:48
  • @Ray http://cs.stackexchange.com/questions/63533/untyped-vs-unityped#63535 – Tim Sep 16 '16 at 14:51
  • As said by @AlexW in his comment, `Programming Language Theory is correct`: JavaScript imposes type restrictions: if not, try `null[0]` and tell me you don't get a type error; so it is a typed language. – Miguel Jun 09 '22 at 14:29
90

strong/weak can be thought of in relation to how the compiler, if applicable, handles typing.

  • Weakly typed means the compiler, if applicable, doesn't enforce correct typing. Without implicit compiler interjection, the instruction will error during run-time.

    "12345" * 1 === 12345  // string * number => number
    

    Strongly typed means there is a compiler, and it wants you an explicit cast from string to integer.

    (int) "12345" * 1 === 12345
    

    In either case, some compiler's features can implicitly alter the instruction during compile-time to do conversions for you, if it can determine that is the right thing to do.

    Thus far, JavaScript can be categorized as Not-Strongly-Typed. That either means it's weakly-typed or un-typed.

dynamic/static can be thought of in relation to how the language instructions manipulate types.

  • Dynamically typed means the value's type is enforced, but the variable simply represents any value of any type.

    x = 12345;    // number
    x = "string"; // string
    x = { key: "value" }; // object
    y = 123 + x; // error or implicit conversion must take place.
    

    Statically typed means the variable type is strongly enforced, and the value type is less-so enforced.

    int x = 12345; // binds x to the type int
    x = "string";  // too late, x is an integer - error
    string y = 123; // error or implicit conversion must take place.
    

    Thus far, JavaScript can be categorized as Not-Statically-Typed. Also, it appears to be Dynamically Typed, if typed at all. So we need to see what Typing means.

Typed means that the language distinguishes between different types such as string, number, boolean, object, array, null, undefined and so on. Also each operation is bound to specific types. So you cannot divide an integer by a string.

    2 / "blah"  // produces NaN

Untyped means the operation of dividing integer by string would result in treating the first four bytes of string as integer. This is because Untyped operations take place directly on bits, there are no types to observe. The outcome will be something quite unexpected:

    2 / "blah"  // will be treated as  2 / 1500275048

Since JavaScript behaves according to the definition of being Typed, it must be. And therefore it must be Dynamically Typed, and Weakly Typed.

If anybody claims JavaScript is Untyped, it is merely for academic theory, not for practical application.

Suamere
  • 5,691
  • 2
  • 44
  • 58
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 1
    But I think there's a contradiction in the last statement. In JavaScript, the result of an integer-string division is well-defined, for any value of the integer or string. It's just not very useful! – Deniz Dogan Jun 08 '09 at 13:45
  • If you have a better definition/description, feel free to edit it. That’s why I made it community wiki. – Gumbo Jun 08 '09 at 13:53
  • @skurpur: You had completely swapped the meanings of Dynamic and Weak typing - corrected that. – Rene Saarsoo Jun 08 '09 at 14:56
  • Oh, sorry, you edited it whyle I was also editing it and I overwrote your changes. – Rene Saarsoo Jun 08 '09 at 15:01
  • 3
    -1. You need to read [this](http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html). – missingfaktor May 15 '12 at 11:56
  • Typing of a variable has nothing to do with casting of values. It has to do with declarations of types for variables. Javascript doesn't have a construct for this at all, which is why so few javascripters even understand the concept, and why so many of them misconstrue variable typing as something to do with comparing or casting variable types. In JS you can't declare `String a = "blah";` or `var a:String = 'blah';` (i.e. typed variables) AT ALL. That is why it is untyped. – Jimbo Jonny Oct 19 '12 at 17:09
50

JavaScript is weakly typed. It is most certainly not "untyped" but its weakly typed nature allows for a lot of flexibility in terms of implicit conversions.

Keep in mind that JavaScript is also dynamically typed. This method of typing allows what is know as "duck typing".

For comparison consider that JavaScript is not strongly typed nor is it statically typed. Sometimes understanding what something isn't can help you see better what it is.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 2
    -1. You need to read [this](http://blogs.perl.org/users/ovid/2010/08/what-to-know-before-debating-type-systems.html). – missingfaktor May 15 '12 at 11:55
  • 1
    -1 JS is untyped. Typing refers to the act of declaring a type for a variable, not whether it has types or casts between them very well. – Jimbo Jonny Oct 19 '12 at 16:55
  • 5
    This answer is much better, and much more accurate, than the top voted answer. – Alex W May 30 '14 at 20:40
  • 3
    @JimboJonny: Not correct. Unless in the presence of an Assembly dev, It wouldn't be too incorrect to state that every language is typed. Untyped means the only operations are directly on bit manipulation. But consider that Javascript has toString() and parseInt() methods. Don't get much more typed than that. – Suamere Dec 11 '15 at 16:45
  • 2
    @Suamere - Here’s a quote from *“Javascript: The Definitive Guide”* from O’Reilly: *“An important difference between JavaScript and languages like Java and C is that JavaScript is untyped. This means, in part, that a JavaScript variable can hold a value of any datatype, unlike a Java or C variable, which can hold only the one particular type of data for which it is declared.”* – Jimbo Jonny Dec 11 '15 at 19:18
  • 2
    @Suamere - From W3C Representative Charlie Kindel: *“VB, VBScript and JavaScript are untyped”* – Jimbo Jonny Dec 11 '15 at 19:19
  • 2
    @Suamere - From Mozilla: *Why it's hard to make JS fast: Because JavaScript is an untyped language. untyped == no type declarations* – Jimbo Jonny Dec 11 '15 at 19:20
  • 1
    Charlie Kindel's quote is taken out of context from a rewritten text from the early 90's about handling parameters. The O'Reilly quote states that a JavaScript **variable can hold a value of any datatype**", that's literally the definition of "dynamically typed". Eich says, "[We] are smart enough to see that values have types (duh!)." and also, "Academic [Peeps] **USE** 'untyped' **TO MEAN** 'no static types.'" As for David Mandelin's quote above untyped == no type declarations, that is also an incorrect use of the word, probably taken from listening to Eich. – Suamere Dec 11 '15 at 20:13
  • 3
    So if you buy into the BS that some dude claims that academic people purposely use the word "untyped" to mean "dynamically typed." Then yes, JavaScript is untyped by all of your proofs. But if you know that "untyped" is **incorrectly** used to represent languages that are truly "dynamically typed", then just call the dang languages "dynamically typed." http://tinyurl.com/czhcv54 – Suamere Dec 11 '15 at 20:13
  • @Suamere In the question you linked to, apparently all answers confirm that "untyped" is indeed used to mean "dynamically typed" in the academic context, so I have no idea why you said 'the BS that some dude claims that academic people purposely use the word "untyped" to mean "dynamically typed."' and 'that "untyped" is incorrectly used to represent languages that are truly "dynamically typed"' This is just simply a fact, not "BS" or anything "incorrect". Of course you may **disagree** with the usage of the word in a practical, non-academic context, but that's a completely different matter. – xji May 16 '20 at 09:43
  • @xji Poor wording on my part. Part of my comment says, "untyped is incorrectly used to represent [dynamically typed languages]". Mathematic Academics dislike the term "dynamically typed" when discussing Type Theory. But we aren't discussing type theory in some a low level calculation, we are talking about how Javascript executes. No programming academic today will intentionally use the term untyped to mean dynamically typed, without qualifying it. A programming-centric publication using untyped in the place of dynamically typed is appeasing Mathematical Academia, or is plainly incorrect. – Suamere May 16 '20 at 15:42
9

The problem here that is confusing a lot of programmers is that definitions like this are not standardized somewhere. The term untyped programming language is ambiguous. Does that refer to a language that has no data types or a language that is a lambda calculus untyped variant?

JavaScript/ECMAScript has a type system and all of its functions' domains will accept any Reference specification type. So that means JavaScript has a single data type, in reality. That is a matter of implementation that is more important to the very advanced JavaScript programmers. The average JavaScript programmer only cares about the abstract language data types that have been specified by ECMAScript.

In the context of the everyday programmer, not the researcher or theoretical computer scientist, the term untyped is a misnomer because most people aren't doing lambda calculus. Thus the term confuses the masses and seems to declare that JavaScript does not have any data types which is simply not true. Anyone who has ever used typeof knows that JavaScript has its own language data types:

var test = "this is text";
typeof(test);

yields

"string"

ECMAScript defines the following eight language types: Undefined, Null, Boolean, String, Symbol, Number, BigInt, and Object.

A more accurate designation for JavaScript would be implicitly typed, dynamically typed, or weakly/loosely typed (or some combination thereof), in that JavaScript uses type coercion in some cases which makes the type implicit because you don't have to explicitly specify the type of your variables. It falls under weakly typed because, unlike some languages which distinguish between float and integer etc, it just uses one number type to encompass all numbers, and makes use of the type coercion mentioned previously[Section 9 of ECMAScript Spec], in strong contrast to a strongly-typed language which would have very specific data types (i.e. you would have to specify int or float).

The definitions of statically and dynamically-typed languages are not standardized, however neither was the size of a byte when computers were beginning to evolve. Static and dynamic typing most often refer to the presence of certain language features. One of which is type-checking at runtime, or what is called dynamic type-checking. If you've used JavaScript, you already know that it definitely waits until runtime to check types, which is why you get TypeError exceptions during execution of your code. Example here

I think the top-voted answer is confusing JavaScript functions' polymorphism with functions that will accept literally anything (as with untyped variants of Lambda Calculus) which is an Association Fallacy.

Ben Aston
  • 53,718
  • 65
  • 205
  • 331
Alex W
  • 37,233
  • 13
  • 109
  • 109
  • It's not a misnomer, but context matters, see http://stackoverflow.com/questions/9154388/does-untyped-also-mean-dynamically-typed-in-the-academic-cs-world/9166519#9166519 – Andreas Rossberg Jun 08 '14 at 23:19
  • 1
    @AndreasRossberg It absolutely *is* a misnomer. What you refer to, in *your shamelessly self-promoted link*, is a type system. The reason the term is a misnomer is because it is ambiguous. Most programmers think *data type* not *type system* when they hear about an *untyped language*. I think [Konrad Rudoph's comment](http://stackoverflow.com/questions/9154388/does-untyped-also-mean-dynamically-typed-in-the-academic-cs-world/9166519#comment-11519736) really drives this point home. – Alex W Jun 09 '14 at 13:45
  • Not sure why you find it "shameless" to refer to my previous answer instead of repeating it here. Also, I clearly said context matters. In contrast to K. Rudolph's complaint, there are perfectly consistent and widely accepted definitions of "type system" in literature, and I quoted one. Of course, you are free to find them confusing in your context, but that doesn't make them "misnomers". – Andreas Rossberg Jun 09 '14 at 17:15
  • 1
    @AndreasRossberg Context is very important here. The top-voted answer says "untyped = no type declarations" which is *clearly* incorrect. What I am saying is that it is a misnomer, in this context. No one mentioned lambda calculus here and to do so is somewhat pretentious in this simple case. – Alex W Jun 11 '14 at 14:56
9

To the author's point JavaScript is also classified as Dynamically typed. Wiki states that Dynamically typed languages are type checked at runtime instead of in a compiler while Weakly Typed refers to the ability to change type on the fly within your code. So yes it is both Dynamically typed AND Weakly typed.

Darren Newton
  • 2,847
  • 1
  • 29
  • 31
1

Remember that JavaScript allows you to ask what is the typeof(your_variable), and compare types: 5==="5" returns false. Thus I don't think you can call it untyped.

It is dynamically and (estimated as) weakly typed. You may want to know it uses Duck typing (see andrew's link) and offers OOP though Prototyping instead of classes and inheritance.

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
instanceof me
  • 38,520
  • 3
  • 31
  • 40
  • Typing of a variable has nothing to do with casting of values. It has to do with declarations of types for variables. Javascript doesn't have a construct for this at all, which is why so few javascripters even understand the concept, and why so many of them misconstrue variable typing as something to do with comparing or casting variable types. In JS you can't declare `String a = "blah";` or `var a:String = 'blah';` (i.e. typed variables) AT ALL. That is why it is untyped. – Jimbo Jonny Oct 19 '12 at 17:07
0

I'd argue JavaScript is strongly and dynamically typed.

But that depends on a bunch of terms which seem to to be loosely defined at best, so the above is true only if you accept the definitions below...

Strong / Weak

  • "Strong" typing prevents operations intended for one type of data from being executed on another type. For example, trying to write to an Nth element of an array, where the object is not an array. Strong typing is required for memory safety.
  • "Weak" is the opposite of strong.

Static / Dynamic

  • "Static" typing checks types before program execution (at compile/transpile time). This requires the type information to be encoded into the language's syntax.
  • "Dynamic" is the oposite of static.

JavaScript will not let you corrupt the memory (hence "strong") but does all the checking and type conversions/coercions at run-time (hence "dynamic").


I find it helpful to think of "strong vs weak" as being orthogonal to "static vs dynamic". There are languages that are strong and static (e.g. C# without unsafe context), strong and dynamic (most "scripting" languages seem to fall into that category), weak and static (C/C++).

Not sure what would be weak and dynamic... assembler, perhaps :)

Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
0

While it is typed (you can ask "typeof someVar" and learn its specific type, it's very weak.

Given:

  var a = "5";

you might say that a is a string. However, if you then write:

  var b = a + 10;

b is an int equal to 15, so a acted just like an int. Of course, you can then write:

  var c = a + "Hello World";

and c will equal "5Hello World", so a is again acting like a string.

Nosredna
  • 83,000
  • 15
  • 95
  • 122
James Curran
  • 101,701
  • 37
  • 181
  • 258
  • 1) Casting a value != typing a variable 2) it's not weak, it's non-existent. You cannot type a variable in javascript. – Jimbo Jonny Oct 19 '12 at 17:04
  • Curious why this answer got two down votes. The definition I've found on weak typing says precisely this: The type of a value is determined based on how it's used. – aioobe Mar 07 '18 at 18:59
  • 1
    Is this accurate though? I get `b` equals `510`: https://ideone.com/BRcSW7 – aioobe Mar 07 '18 at 19:13
  • b equals 510 because the 10 is effected by type coercion. if you add a number and a string the result is a string. this still exemplifies why JS is a loosely typed language but yes... the final result is still "5Hello World" because a is still '5' in memory. his answer was half correct lol – Robert O'Toole Mar 02 '21 at 01:02
-1

another interesting example of loosely typed JS:

console.log(typeof(typeof(5)))

the result is string. why? because the initial typeof results in 'integer' which in itself is a string. I would assume in a strongly typed language this type of changing types would not be a thing. perhaps i am mistaken but that was the first instance where i started to understand how CRAZY JS can be lol

Robert O'Toole
  • 197
  • 1
  • 11