How to check if the constructor of an object is a JavaScript Object?

In this article, we will check whether the constructor of an object is a JavaScript Object. The constructor property of any JavaScript variable returns a reference to the Object constructor function that created the instance object. The value of this property is a reference to the function itself.

All objects have the constructor property, and objects created without an explicit constructor function will have a constructor property that points to the fundamental Object constructor type for that object.

To check whether the constructor of a provided value is an Object created by the object constructor function, we need to compare the value of the object's constructor property with the corresponding Object constructor function reference.

Syntax

Following is the syntax of the function to check if the constructor of an object is Object:

function check(obj) {
   return obj.constructor === Object ? true : false;
}

Example: Checking Multiple Object Types

In the program below, we check six objects if their constructor is JavaScript Object:

<html>
<body>
   <h3>Check if Constructor is Object</h3>
   <p>Click on the check button to test all cases</p>
   
   <p>Test Case 1: {} </p>
   <p>Constructor is Object: <span id="testcase1"></p>
   
   <p>Test Case 2: new Number(3)</p>
   <p>Constructor is Object: <span id="testcase2"></p>
   
   <p>Test Case 3: new Object</p>
   <p>Constructor is Object: <span id="testcase3"></p>
   
   <p>Test Case 4: new Object()</p>
   <p>Constructor is Object: <span id="testcase4"></p>
   
   <p>Test Case 5: []</p>
   <p>Constructor is Object: <span id="testcase5"></p>
   
   <p>Test Case 6: "Object Constructor"</p>
   <p>Constructor is Object: <span id="testcase6"></p>
   
   <button onclick="runTestCases()">Check</button>
   
   <script>
      // This function will check if created by Object constructor
      function check(obj) {
         return obj.constructor === Object ? true : false;
      }
      
      function runTestCases() {
         document.getElementById("testcase1").textContent = check({});
         document.getElementById("testcase2").textContent = check(new Number(3));
         document.getElementById("testcase3").textContent = check(new Object);
         document.getElementById("testcase4").textContent = check(new Object());
         document.getElementById("testcase5").textContent = check([]);
         document.getElementById("testcase6").textContent = check("Object Constructor");
      }
   </script>
</body>
</html>

On clicking the "check" button, all test cases will run and show the output as true or false. Test cases 1, 3, and 4 return true because they are created using the Object constructor. Test case 2 returns false because it's a Number object, test case 5 returns false because it's an Array, and test case 6 returns false because it's a String.

Example: Finding Constructor of Different Objects

In the program below, we find the constructor of four different objects created using different methods:

<html>
<body>
   <h3>Find the Constructor of Objects</h3>
   <p id="demo"></p>
   
   <script>
      function Student(first, last, course) {
         this.firstName = first;
         this.lastName = last;
         this.course = course;
      }
      
      const stud1 = new Student("John", "Doe", "M.Tech");
      const stud2 = new Object();
      const stud3 = new Object;
      var stud4 = {
         firstName: "John",
         lastName: "Doe",
         course: "M.Tech"
      };
      
      document.getElementById("demo").innerHTML += `Constructor of stud1: ${stud1.constructor}<br>`;
      document.getElementById("demo").innerHTML += `<br>Constructor of stud2: ${stud2.constructor}<br>`;
      document.getElementById("demo").innerHTML += `<br>Constructor of stud3: ${stud3.constructor}<br>`;
      document.getElementById("demo").innerHTML += `<br>Constructor of stud4: ${stud4.constructor}<br>`;
   </script>
</body>
</html>

Key Points

When checking constructors, remember:

  • Objects created with {} and new Object() have Object as constructor
  • Arrays, strings, and numbers have their respective constructors (Array, String, Number)
  • Custom constructor functions create objects with that function as constructor
  • The constructor property can be modified, so this method isn't foolproof for security checks

Conclusion

Checking if an object's constructor is Object helps identify plain objects versus other types. Use obj.constructor === Object to perform this check, but be aware that constructor properties can be modified.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements