How to Get the Type of a Variable in JavaScript

TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It offers classes, modules, and interfaces to help you build robust components.

Checkout this video:


In JavaScript, there are 6 primitive data types: string, number, boolean, null, undefined, symbol (added in ES6), and 2 composite data types: object and function.

You can use typeof to check the type of a variable, as follows:

let foo = 42; //foo is now a Number
console.log(typeof foo); //Prints “number” to the console

let bar = “Hello world!”; //bar is now a String
console.log(typeof bar); //Prints “string” to the console

let baz = true; //baz is now a Boolean
console.log(typeof baz); //Prints “boolean” to the console

In addition to these 6 primitive types, there are also 2 composite types: object and function. Objects are variables that can contain multiple values, and functions are self-contained blocks of code that can be executed on demand.

Using typeof

In JavaScript, there are 6 primitive data types: string, number, boolean, null, undefined, symbol (new in ECMAScript 2015).

typeof is a unary operator that is placed before its single operand, which can be of any type. This operator returns a string indicating the type of the operand.

The six possible values returned by typeof are: “string”, “number”, “boolean”, “undefined”, “object”, and “symbol” (new in ECMAScript 2015).

typeof “foo” // returns “string”
typeof new String(“foo”) // also returns “string”

typeof 12345 // returns “number”
typeof 3.14159 // also returns “number”

typeof true // returns “boolean”

// everything else returns false:
typeof false // boolean false value (instead of true)

// everything else except objects return true: Type coercion will convert null and undefined to false. Since they are not objects, type coercion doesn’t apply to them. You can use === instead to check for their equality with false. Conversely !== can be used to check for their inequality with true.

has only two possible values: true or false.

Using instanceof

The instanceof operator returns true if the specified object is an instance of the given constructor. So, in order to find out whether a variable is an array, we can use the following code:

var myArray = [1, 2, 3];
console.log(myArray instanceof Array); // true


In conclusion, you can use the typeof operator to get the type of a variable in JavaScript. However, there are some caveats that you should be aware of. For example, typeof null will return ‘object’, and typeof an array will return ‘object’.

If you want to be sure that you’re getting the accurate type of a variable, you can use the Object.prototype.toString method. This method will return ‘[object Type]’, where Type is the type of the variable.

Remember, getting the type of a variable is important in JavaScript because variables can hold any data type, and understanding what data type a variable contains is crucial in write correct code.

Scroll to Top