JavaScript Helper to determine the type of your variable

Continuously you will need to know what type are the variables in order to create conditionals that are prepared to manipulate and maintain your code stable. For that reason I give you a cheat sheet to establish what is exactly your variable.

These are functions to which you pass your variable to indicate whether it is of a certain type or not.


const myObject = {};


const myArray = [];

Number, String, Boolean and Function

For these 4, it’s more simple. We just need a typeof following === and their type as string to make the condition.

const myNumber = 12;
const myString = "js";
const myBoolean = true;
const myFunction = () => "hello";

Null and Undefined

Because null == undefined is true, the bellow code will catch both null and undefined.

const myNull = null;

which is 100% equivalent to the more explicit but less concise:

if (variable === undefined || variable === null) 

Note: We are not using typeof on purpose because this solution above will cause a ReferenceError if the variable doesn't exist. This is a good thing. This ReferenceError is desirable in order to help and find any mistakes and fix them before shipping your code.


Object  variable.constructor === Object



Software Engineering student @Flatiron

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store