JavaScript is a dynamically-typed language. These means that types are checked at runtime when you try to do something with a variable, rather than by a compiler. For example, the following is valid JavaScript code:
var myVariable = 0; console.log(typeof myVariable); // Prints "number" myVariable = "1"; console.log(typeof myVariable); // Prints "string"
Although variables do have a type, this may change throughout the lifetime of the variable.
JavaScript also tries to implicitly convert types where possible, for example, using the equality operator:
console.log(2 == "2"); // Prints "true"
Although this might make sense for frontend JavaScript (for example comparing against the value of a form input), in general, it is more likely to be a source of errors or confusion. For this reason, it is recommended to always use the strict equality and inequality operators:
console.log(2 === "2"); // Prints "false" console.log(2 !== "2"); // Prints "true"