JavaScript is loosely typed, because any JavaScript variable can store any type of data.
Here's an example, where someVariable
can store either a number
or a string
:
var someVariable = 33;
console.log(someVariable); // 33
someVariable = 'some string';
console.log(someVariable); // someVariable
This is contrary to strongly typed languages, where variables have to declare which type of data they store. Trying to store anything else produces an error.
For example, in TypeScript
:
var someVariable: number = 33;
// Type 'string' is not assignable to type 'number'
someVariable = 'some string';