What does Loosely Typed mean?
Almost every programming language has a concept called variables which are nothing but containers to hold data of different types like integer, string etc. In traditional strongly typed programming languages like C, C++, Java etc the way you create these variables decide what type of data it is going to hold through out the program and if you try to assign data which is not the designated type the compiler throws an Error.
int i; // variable that can hold only an integer i = 10; // Works fine i = "John Doe"; // Compiler Error due to Strongly Typed nature
var i; // variable that can contain any type of data i = 10; // Works fine i = "John Doe"; // no error due to Loosely Typed nature of JS
- Primitive Types
- Reference/Object Types
See Primitive vs Reference types for a deeper insight.
- Boolean (true/false)
All reference types can be identified using instanceof operator except for Arrays which might not be correctly identified in some cases. Hence it is recommended to use Array.isArray() to check for an Array as shown in the code example below:
NOTE: typeof operator doesn’t work well with reference types hence it is recommended to use instanceof instead.