Type checking in JavaScript: typeof and instanceof operators
JavaScript is a loosely-typed language, so there is no restriction on the variable’s type. For example, if you’ve created a variable with a string type, later you can assign
7.3k
By Kate Angelou
JavaScript is a loosely-typed language, so there is no restriction on the variable’s type.
For example, if you’ve created a variable with a string type, later you can assign to the same variable a number:
let message ='Hello';
message =14;
Such dynamism gives you flexibility and simplifies variables declaration.
On the other side, you can never be sure that a variable contains a value of a certain type. For example, the following function greet(who) expects a string argument, however, you can invoke the function with any type of argument:
That’s why, sometimes, you need to check the variable’s type in JavaScript — using typeof operator, as well as instanceof to check instance types.
Let’s see in more detail how to use typeof and instanceof operators in JavaScript.
1. typeof operator
In JavaScript, you can find primitive types like strings, numbers, booleans, symbols. Additionally, there are functions, objects, and the special values undefined and null.
typeof is the operator that let’s you determine the type of the expression:
const typeAsString =typeof expression;
where expression evaluates to a value which type you’d like to find. expression can be a variable myVariable, property accessor myObject.myProp, function invocation myFunction(), or even a raw literal 14.
typeof expression, depending on the value of expression, evaluates to one of the strings: 'string', 'number', 'boolean', 'symbol', 'undefined', 'object', 'function'.
Let’s see how typeof operator works for each type:
While usually typeof expression determines the type of expression, you can use typeof also to determine if a variable is defined or not.
JavaScript throws a reference error if you access a variable that is not defined:
notDefinedVar;
But typeof has a nice property — a reference error is not thrown when typeof evaluates the type of a not defined variable:
typeof notDefinedVar;
The variable notDefinedVar is not defined in the current scope. However, typeof notDefinedVar doesn’t throw a reference error, and evaluates to 'undefined' string.
You can use typeof to detect if a variable is not defined: typeof myVar === 'undefined' evaluates to true if myVar is not defined.
But JavaScript functions can do more: they can even construct objects! To make a function construct objects, just use new keyword before the regular function invocation:
new Greeter('World') is a constructor invocation that creates the instance worldGreeter.
How can you check in JavaScript that a certain instance was created with a certain constructor? Welcome instanceof operator:
const bool = object instanceofConstructor;
where object is an expression that evaluates to an object, Contructor is a class or function that constructs objects. instanceof evaluates to a boolean.
worldGreeter instance was created using Greeter constructor, so worldGreeter instanceof Greeter evaluates to true.
Starting ES2015, a better way to construct objects is by using the class syntax. For example, let’s define a class Pet and then created an instance of it myPet:
As for more practical examples, you may find instanceof useful to determine the built-in special instances like regular expressions, arrays:
functionisRegExp(value){return value instanceofRegExp;}isRegExp(/Hello/);isRegExp('Hello');functionisArray(value){return value instanceofArray;}isArray([1,2,3]);isArray({ prop:'Val'});
But at the same time, myCat is also an instance of the base class Pet!
myCat instanceofPet;
In simple words, object instanceof Constructor evaluates to true if object is an instance of Constructor, but also if Constructor is the parent class of instance’s class.
3. Summary
JavaScript is a loosely-typed language, meaning that there is no restriction on what type a variable can have.
Thus, sometimes, you have to check what type the variable has.
typeof expression is the operator that lets you determine the type of expression. typeof evaluates to one of the values: 'string', 'number', 'boolean', 'symbol', 'undefined', 'object', 'function'.
typeof null evaluates to 'object', thus the correct way to use typeof to detect an object is typeof object === 'object' && object !== null.
instanceof operator let’s identify the instance’s constructor. object instanceof Constructor evaluates to true if object is an instance of Constructor.
Quiz: What is the built-in constructor for which instanceof for any object returns true?