Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to check if an object is an instance of a Class in JavaScript?
In JavaScript, you can check if an object is an instance of a specific class using the instanceof operator. This operator returns true if the object was created by the specified constructor function or class.
Syntax
object instanceof Constructor
Using instanceof with Constructor Functions
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Instance Check Example</title>
</head>
<body>
<button onclick="checkInstance()">CHECK INSTANCE</button>
<script>
function Student(name, age, standard) {
this.name = name;
this.age = age;
this.standard = standard;
}
let student1 = new Student("Rohan", 18, 12);
let regularObject = { name: "John", age: 20 };
function checkInstance() {
let result = document.getElementById("result");
if (student1 instanceof Student) {
result.innerHTML = "student1 is an instance of Student: true<br>";
} else {
result.innerHTML = "student1 is not an instance of Student: false<br>";
}
result.innerHTML += "regularObject instanceof Student: " + (regularObject instanceof Student);
}
</script>
</body>
</html>
student1 is an instance of Student: true regularObject instanceof Student: false
Using instanceof with ES6 Classes
<!DOCTYPE html>
<html>
<body>
<script>
class Animal {
constructor(name) {
this.name = name;
}
}
class Dog extends Animal {
constructor(name, breed) {
super(name);
this.breed = breed;
}
}
let myDog = new Dog("Max", "Labrador");
let myAnimal = new Animal("Generic");
document.getElementById("output").innerHTML =
"myDog instanceof Dog: " + (myDog instanceof Dog) + "<br>" +
"myDog instanceof Animal: " + (myDog instanceof Animal) + "<br>" +
"myAnimal instanceof Dog: " + (myAnimal instanceof Dog);
</script>
</body>
</html>
myDog instanceof Dog: true myDog instanceof Animal: true myAnimal instanceof Dog: false
Built-in Types Example
<!DOCTYPE html>
<html>
<body>
<script>
let arr = [1, 2, 3];
let obj = { key: "value" };
let date = new Date();
document.getElementById("types-result").innerHTML =
"Array instanceof Array: " + (arr instanceof Array) + "<br>" +
"Object instanceof Object: " + (obj instanceof Object) + "<br>" +
"Date instanceof Date: " + (date instanceof Date) + "<br>" +
"Array instanceof Object: " + (arr instanceof Object);
</script>
</body>
</html>
Array instanceof Array: true Object instanceof Object: true Date instanceof Date: true Array instanceof Object: true
Alternative Methods
Besides instanceof, you can also use:
<!DOCTYPE html>
<html>
<body>
<script>
function Student(name) {
this.name = name;
}
let student = new Student("Alice");
// Using constructor property
let isStudentByConstructor = student.constructor === Student;
// Using Object.getPrototypeOf()
let isStudentByPrototype = Object.getPrototypeOf(student) === Student.prototype;
document.getElementById("alternative-result").innerHTML =
"Using constructor: " + isStudentByConstructor + "<br>" +
"Using prototype: " + isStudentByPrototype + "<br>" +
"Using instanceof: " + (student instanceof Student);
</script>
</body>
</html>
Using constructor: true Using prototype: true Using instanceof: true
Key Points
-
instanceofchecks the entire prototype chain, not just the immediate constructor - It works with both constructor functions and ES6 classes
- Returns
falsefor primitive values andnull - Inheritance relationships are properly detected
Conclusion
The instanceof operator is the standard way to check if an object is an instance of a specific class in JavaScript. It works with constructor functions, ES6 classes, and built-in types, making it versatile for type checking.
Advertisements
