Javascript is dynamically typed , while other languages, such as C # and Java, are statically typed. This means that in Javascript variables, you can reassign values ββof any type and, therefore, you do not need to explicitly specify the type of variables or the type of returned functions. If you saw such code in a statically typed language
int x = 5; x = "hello";
you rightfully expect the compiler to start throwing a big nasty TypeError
. Javascript, on the other hand, will successfully run this code, even if the type has changed.
var x = 5; x = "hello";
Since variables can change types, the compiler cannot know so much information about them. You should expect Javascript tools to be worse than Java / C #, as much as subtleties such as code completion are useful. Fewer errors will be detected at compile time, and you will have to do more debugging at runtime than you probably used.
However, it also allows you to be more free with your variables, and you can change types as you wish, which is often convenient. You can write this code if you want:
var x; //typeof x === "undefined" x = "Hello, world!"; //typeof x === "string" x = 42; //typeof x === "number" x = false; //typeof x === "boolean" x = {}; //typeof x === "object"
source share