
In Javascript, one variable can hold multiple value types within the same file as shown below: let x = 1 However, this becomes even more essential for accuracy when it comes to dynamically typed languages such as Javascript. var a = Ĭonsole.Checking data types for variables is vital in any programming language to ensure a smooth and error-free development process. This will check whether the value is a specific "kind" of an object. Ths can be done with the instanceof operator. However sometimes we need to check whether the value represents an array or not. Typeof operator on an array will return "object" (as Javascript array is an object internally). Typeof operator will return "object" if the value represents an object. Typeof operator will return "boolean" for boolean values (true / false). Typeof operator will return "string" for a string of characters.

Typeof operator will return "number" for a numeric value. So we need to use the strict equality operator = to check for a null. Unfortunately due to a Javascript bug, typeof operator on null will return the type as "object". var a Īlternatively you can use the strict equality operator to check for undefined. Typeof operator will return "undefined" for an undefined value.

Instanceof will return a boolean true / false depending on whether the value is an instance of a given object or not. Using this we can check whether a given value represents an array. For example, Javascript arrays are basically objects. Instanceof : This checks the "kind" of an object. It will return a string specifying the type - "undefined" / "string" / "number" / "boolean" / "object" etc. Typeof : This checks whether the value is one of the primitive data types. Javascript provides 2 operators to check the type of a given value :

