0

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?

TheFallenOne
  • 1,598
  • 2
  • 23
  • 57

2 Answers2

1

The reason for this is so dynamic types can be used. Thus, this following code, whose 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, 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.