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?
2 Answers
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.
Comments
Javascript is a loosely-typed language so variables types are determined at runtime instead of at compile time.
1 Comment
Aadit M Shah
Variables don't have types. Only values do.
var a = "abc";a=5;is valid code