How to check the type of a variable in JavaScript

Published on

Just because you don't have to declare your types in your JavaScript code doesn't mean that they aren't there at runtime. JavaScript is both weakly and dynamically typed which allows for a lot of flexibility in terms of implicit conversions. Let's look at an example:

const message = 'Hello World!';

typeof message; // "string"

Here we're using the typeof operator to get the underlying type for our variable message. In this instance it's returned 'string' but there's actually a total of 6 values it can return.

typeof 'Hello World!'; // "string"

typeof 2; // "number"

typeof true; // "boolean"

typeof {}; // "object"

typeof function () {}; // "function"

typeof undeclaredVar; // "undefined"

We can use a different method instanceof to check if a variable is of the type specified

const person = new Person();
person instanceof Person; // true
person instanceof String; // false
person instanceof Vehicle; // false

What it's really doing here is just testing if the provided function's prototype is in the object's prototype chain.