I have just started using JavaScript and it just seems strange to me that when the variable is declared,we don't have to specify the datatype. Is there any specific reason this is done?
Asked
Active
Viewed 121 times
0
-
JavaScript doesn't really care about that stuff. `var a = "abc";a=5;` is valid code – Downgoat Apr 08 '15 at 04:03
-
JavaScript is **not** a strictly typed language (at least, yet). – PM 77-1 Apr 08 '15 at 04:03
2 Answers
1
The reason for this is so dynamic types can be used. Thus, this following code, whose c counterpart would be invalid, works:
var x; // Now x is undefined
var x = 5; // Now x is a Number
var x = "John"; // Now x is a String
Thus, javascript is not a strictly typed language.

A.J. Uppal
- 19,117
- 6
- 45
- 76
-1
Javascript is a loosely-typed language so variables types are determined at runtime instead of at compile time.