JavaScript - check type of object
In this article, we are going to show how to check object type in JavaScript.
Quick solution:
// ONLINE-RUNNER:browser;
var variable = null;
console.log( typeof variable ); // object <--- with typeof
console.log( variable instanceof Object ); // false (for Object) <--- with instanceof
console.log( getType(variable) ); // Null <--- with toString
// Helper util:
function getType(object) {
var text = Object.prototype.toString.call(object);
return text.substring(8, text.length - 1);
}
There are three different ways how to do it.
The most common way is to use typeof
or instanceof
operators, rarely toString()
:
typeof variable |
| variable.toString() |
Result type: e.g.
|
Result type: |
Result type: e.g.
|
Check the below table to see an example results summary of different approaches.
Note: in the first impression the results of the operator could be strange.
Summary:
variable typeof toString instanceof
null object Null false (for Object)
undefined undefined Undefined false (for Object)
{ } object Object true (for Object)
new Object() object Object true (for Object)
new Student() object Object true (for Student) // custom class used
new function() { } object Object true (for Object)
[ ] object Array true (for Array)
new Array() object Array true (for Array)
true boolean Boolean false (for Boolean)
Boolean(true) boolean Boolean false (for Boolean)
new Boolean(true) object Boolean true (for Boolean)
5 number Number false (for Number)
Number(5) number Number false (for Number)
new Number(5) object Number true (for Number)
5n bigint BigInt false (for BigInt)
BigInt(5) bigint BigInt false (for BigInt)
'a' string String false (for String)
String('a') string String false (for String)
new String('a') object String true (for String)
function() { } function Function true (for Function)
Date function Function true (for Function)
RegExp function Function true (for Function)
Function function Function true (for Function)
new Function('') function Function true (for Function)
new Date() object Date true (for Date)
/[a-z]/g object RegExp true (for RegExp)
new RegExp('[a-z]', 'g') object RegExp true (for RegExp)
1. typeof
operator example
In this section, we use typeof
operator to show what kind of type is some object.
Simple types and undefined
were marked in comments with ----->
.
// ONLINE-RUNNER:browser;
function Student() {
// class logic...
}
console.log(typeof( null )); // object
console.log(typeof( undefined )); // -----> undefined
console.log(typeof( { } )); // object
console.log(typeof( new Object() )); // object
console.log(typeof( new Student() )); // object
console.log(typeof( new function() { } )); // object
console.log(typeof( [ ] )); // object
console.log(typeof( new Array() )); // object
console.log(typeof( true )); // -----> boolean
console.log(typeof( Boolean(true) )); // -----> boolean
console.log(typeof( new Boolean(true) )); // object
console.log(typeof( 5 )); // -----> number
console.log(typeof( Number(5) )); // -----> number
console.log(typeof( new Number(5) )); // object
console.log(typeof( 5n )); // -----> bigint
console.log(typeof( BigInt(5) )); // -----> bigint
console.log(typeof( 'a' )); // -----> string
console.log(typeof( String('a') )); // -----> string
console.log(typeof( new String('a') )); // object
console.log(typeof( function() { } )); // function
console.log(typeof( Date )); // function
console.log(typeof( RegExp )); // function
console.log(typeof( Function )); // function
console.log(typeof( new Function('') )); // function
console.log(typeof( new Date() )); // object
console.log(typeof( /[a-z]/g )); // object
console.log(typeof( new RegExp('[a-z]', 'g') )); // object
2. Object.prototype.toString
method example
In this section, we used toString
method to show how to get a type name. The reason why we used Object.prototype.toString.call()
approach to call it is the prevention of local method overloading.
// ONLINE-RUNNER:browser;
function getType(object) {
var text = Object.prototype.toString.call(object);
return text.substring(8, text.length - 1);
}
// Example:
function Student() {
// class logic...
}
console.log(getType( null )); // Null
console.log(getType( undefined )); // Undefined
console.log(getType( { } )); // Object
console.log(getType( new Object() )); // Object
console.log(getType( new Student() )); // Object
console.log(getType( new function() { } )); // Object
console.log(getType( [ ] )); // Array
console.log(getType( new Array() )); // Array
console.log(getType( true )); // Boolean
console.log(getType( Boolean(true) )); // Boolean
console.log(getType( new Boolean(true) )); // Boolean
console.log(getType( 5 )); // Number
console.log(getType( Number(5) )); // Number
console.log(getType( new Number(5) )); // Number
console.log(getType( 5n )); // BigInt
console.log(getType( BigInt(5) )); // BigInt
console.log(getType( 'a' )); // String
console.log(getType( String('a') )); // String
console.log(getType( new String('a') )); // String
console.log(getType( function() { } )); // Function
console.log(getType( Date )); // Function
console.log(getType( RegExp )); // Function
console.log(getType( Function )); // Function
console.log(getType( new Function('') )); // Function
console.log(getType( new Date() )); // Date
console.log(getType( /[a-z]/g )); // RegExp
console.log(getType( new RegExp('[a-z]', 'g') )); // RegExp
3. instanceof
operator example
Another way to check the type of object is to use instanceof
operator that returns true
or false
.
false
results were marked in comments with ----->
.
Note: main disadvantage of this approach is we are able just to test objest with some type.
// ONLINE-RUNNER:browser;
function Student() {
// class logic...
}
console.log( null instanceof Object ); // -----> false
console.log( undefined instanceof Object ); // -----> false
console.log( { } instanceof Object ); // true
console.log( new Object() instanceof Object ); // true
console.log( new Student() instanceof Student ); // true
console.log( new function() { } instanceof Object ); // true
console.log( [ ] instanceof Array ); // true
console.log( new Array() instanceof Array ); // true
console.log( true instanceof Boolean ); // -----> false
console.log( Boolean(true) instanceof Boolean ); // -----> false
console.log( new Boolean(true) instanceof Boolean ); // true
console.log( 5 instanceof Number ); // -----> false
console.log( Number(5) instanceof Number ); // -----> false
console.log( new Number(5) instanceof Number ); // true
console.log( 5n instanceof BigInt ); // -----> false
console.log( BigInt(5) instanceof BigInt ); // -----> false
console.log( 'a' instanceof String ); // -----> false
console.log( String('a') instanceof String ); // -----> false
console.log( new String('a') instanceof String ); // true
console.log( function() { } instanceof Function ); // true
console.log( Date instanceof Function ); // true
console.log( RegExp instanceof Function ); // true
console.log( Function instanceof Function ); // true
console.log( new Function('') instanceof Function ); // true
console.log( new Date() instanceof Date ); // true
console.log( /[a-z]/g instanceof RegExp ); // true
console.log( new RegExp('[a-z]', 'g') instanceof RegExp ); // true