Hi
I've found that some people call JavaScript a "dynamically, weakly typed" language, but some even say "untyped"? Which is it really?
Hi
I've found that some people call JavaScript a "dynamically, weakly typed" language, but some even say "untyped"? Which is it really?
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.
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.
Typing system
Weakly typed means type conversion is done implicitly behind the scenes:
"12345" * 1 === 12345 // string * number => number
In strongly typed lanugages you would need to explicitly cast the string to integer:
(int) "12345" * 1 === 12345
Dynamically typed means a variable can hold values of different data types:
x = 12345; // number
x = "string"; // string
In statically typed languages a variable can only have values of the type it was declared for:
int x = 12345; // binds x to the type int
x = "string" // error
Typed means that the language distinguishes between different types such as integer, float, string, boolean and so on. Also each operation is bound to specific types. So you cannot divide an integer by a string.
2 / "blah" // produces NaN
In untyped languages the operation of dividing integer by string would result in treating the first four bytes of string as integer and the outcome will be something quite unexpected:
2 / "blah" // will be treated as 2 / 1500275048
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.
Javascript is not untyped:
http://en.wikipedia.org/wiki/Programming_language#Typed_versus_untyped_languages
an untyped language, such as most assembly languages, allows any operation to be performed on any data, which are generally considered to be sequences of bits of various lengths. High-level languages which are untyped include BCPL and some varieties of Forth.
So for example, in Javascript, it cannot be
var flag = 3 in 3;
(Note: "in" is an operator. This one will have an invalid operand error). Therefore Javascript won't allow any operation to be performed on any data, and therefore is not untyped.
Update: thanks for pointing out "hello" / 3 is... NaN... we might say that it is allowed.
If a language were "untyped", there would be no concept of types in the language -- that is, no concept of string
, number
, int
, etc. The bottom line is, in an "untyped" language, it will come down to shuffling bits around without a concept of that the value represents.
A data type is an abstraction -- it gives meaning to data, a bunch of bits, to represent some kind of tangible thing, such as a string
or number
.
For example, manipulating the bits stored in memory by electrical signals is basically performing operations on untyped data -- it basically just deals with switching the electrical states of memory cells on and off. Even at that very low level, even the concept of a "byte" can be thought as a data type, as it represents a group of 8 bits.
The Wikipedia article on type system has more information on what it entails.
JavaScript is not an untyped language, as it has the concept of strings, numerical values and such that is associated with a value. And there are rules for what kind of operations that can be performed on the different types of values. Therefore, it is a typed language.
JavaScript is dynamically typed, as the type of the variable is determined at runtime, not at compile-time. As the type is determined at runtime, it does not require the explicit declaration of types on variables unlike statically typed languages which performs type checks at compile-time.
Dynamically typed:
var x = "hello" // The variable itself has no type associated to it.
// But, the value "Hello" has a type --> it's a string.
x = 10 // As the variable itself has no type, it can be assigned
// a value of differing types later on.
// This is a fairly distinct characteristic of a dynamically
// typed language.
Statically typed:
string s = "hello"; // The variable has a type, as does the data itself.
s = 10 // Error! -- The variable "s" is assigned a data type of
// "string", it cannot accept non-string values.
Furthermore, JavaScript is also weakly typed, meaning that operations can be performed on values of differing types. This may mean that the language will have implicit casts of types to allow certain operations to occur. This is opposed to strongly typed languages which restricts operations which can be performed on values of differing data types.
Weakly typed:
x = "Hello" + 10 // Operations can be performed on values of differing
// types without a problem. Here, we expect that the
// numerical value 10 will be implicitly cast to a
// string value "10"
Strongly typed:
x = "Hello" + 10 // Error! Operations cannot be performed on values
// of differing types. The idea of adding a string and
// numerical value does not make sense in this language.
To get an overall idea of how all these type system concepts come together, here's a little example with an explanation of each part:
var s = "hello" // "hello" is a string: JavaScript is typed.
var x = 10 + s // 10 is a numerical value: JavaScript is typed.
^ |____|
| ^ // 10 and s are different types but can be added together:
| L______ // Javascript is weakly typed.
|
L_______ // x does not have a type associated with it:
// JavaScript is dynamically typed.
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 Protoyping instead of classes and inheritance.