Check Type of Variable in JavaScript with typeof [Examples]

JavaScript typeof Operator

This article will explain JavaScript variable types and how to find the type of a variable using the typeof operator.

The typeof operator is quite similar to the instanceof operator – but they do not function the same wayinstanceof returns TRUE or FALSE when checking if a variable is of a certain type, whereas typeof returns the name of the type.

What are Types?

The type of a variable determines what it can or can’t do.

It determines what value the variable may take and what can be done with that value.

For example, a number type variable containing a numeric value can be used in arithmetic. In contrast, a string type variable containing a sequence of words cannot be used in arithmetic calculations, but it can be joined to other strings to create longer strings.

Finding a Variable’s Type in JavaScript

You will usually know what a variable’s type will be in advance – after all, you’ll be the one declaring it and assigning it a value.

On occasion, though, you may not. For example, the value may come from a third-party package, or be retrieved via an API call, or come from user input – and you may want to determine the variable’s type before you try and do something with it.

The typeof Operator

The typeof operator does just that – it tells you the type of a given variable. Here’s the syntax:

typeof operand

Note that:

  • typeof is an operator, not a function
    • This means that you call it and follow it with a variable or value separated by a space
  • operand should be the variable or value you wish to check the type of
  • A string will be returned containing the name of the type (See the below table for possible return values)
Variable Type typeof Output
Undefined “undefined”
Null “object”
Boolean “boolean”
Number “number”
BigInt “bigint”
String “string”
Symbol “symbol”
Function object “function”
Any other object “object”

Examples

Below, you can see what value the typeof operator returns based on various values passed to it:

typeof 32; // 'number'
typeof 1.89; // 'number'
typeof NaN; // 'number' - this variable should be numeric, but a numeric value was not able to be assigned to it, so it is a number variable with a NaN (Not a Number) value to indicate as such

typeof ''; // 'string'
typeof 'foo'; // 'string'

typeof true; // 'boolean'
typeof false; // 'boolean'

typeof undefined; // 'undefined'

typeof {property: 'value'}; // 'object'

Checking if a Variable is a Certain Type

Based on the output demonstrated above, you can compare the output of typeof to a known value to perform actions based on a variable’s type.

var myVariable = 64;
if(typeof myVariable === 'number'){
    console.log('The variable is a number')
}

This is especially useful when checking that a variable has been assigned a value:

var myVariable; 
if(typeof myVariable === 'undefined'){
    // Variable is undefined, so no value has been defined though the variable has been declared
    console.log('No value has been assigned to the variable')
} else {
    // Value is assigned, you can do something with the variable here
}

For more information on the typeof operator, you can check out the official Mozilla developer resource at:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

Your email address will not be published. Required fields are marked *