Check If a Value Is Object-Like in JavaScript



In JavaScript, an object-like value is a value that is not a primitive value and is not undefined. An object-like value is any value that is not a primitive value, including functions, arrays, and objects. There are different methods for checking if a value is objectlike in JavaScript. In this article, we are going to look at 3 methods for checking if a value is object-like in JavaScript.

Using the typeof Operator

The typeof operator is a built-in operator in JavaScript that is used to check the type of a value. The typeof operator returns a string that is the type of the value. The typeof operator can be used to check if a value is object-like in JavaScript.

Example 1

<!doctype html>
<html>
<head>
   <title>Examples</title>
</head>
<body>
   <div id="result1"></div>
   <div id="result2"></div>
   <div id="result3"></div>
   <script>
      document.getElementById("result1").innerHTML ="typeof {}: " + typeof {};
      document.getElementById("result2").innerHTML ="typeof []: " + typeof [];
      document.getElementById("result3").innerHTML ="typeof function(){}: " + typeof function(){};
   </script>
</body>
</html>

Below is the detailed explanation for the above code snippet:

In the first statement in the script tag, we are checking the type of an empty object {}. The typeof operator returns the string "object" for an empty object. In the second statement, we are checking the type of an empty array []. The typeof operator returns the string "object" for an empty array. In the third statement, we are checking the type of an anonymous function function(){}. The typeof operator returns the string "function" for an anonymous function.

Using the instanceof Operator:

The instanceof operator is a built-in operator in JavaScript that is used to check if a value is an instance of a constructor function. The instanceof operator returns a Boolean value, which is true if the value is an instance of the constructor function, and false if the value is not an instance of the constructor function. The instanceof operator can be used to check if a value is object-like in JavaScript.

Example 2

<!doctype html>
<html>
<head>
   <title>Examples</title>
</head>
<body>
   <div id="result1"></div>
   <div id="result2"></div>
   <div id="result3"></div>
   <script>
      document.getElementById("result1").innerHTML = "Is {} an instance of Object: " + ({} instanceof Object);
      document.getElementById("result2").innerHTML = "Is [] an instance of Array: " +([] instanceof Array);
      document.getElementById("result3").innerHTML ="Is function(){} is an instance of Function: " + (function(){} instanceof Function);
   </script>
</body>
</html>

Within the script tag, in the first statement, we are checking if an empty object {} is an instance of the Object constructor function. The instanceof operator returns the Boolean value true for an empty object. In the second statement, we are checking if an empty array [] is an instance of the Array constructor function. The instanceof operator returns the Boolean value true for an empty array. In the third statement, we are checking if an anonymous function function(){} is an instance of the Function constructor function. The instanceof operator returns the Boolean value true for an anonymous function.

The Object.prototype.toString() method:

The Object.prototype.toString() method is a built-in method in JavaScript that is used to convert an object to a string. The Object.prototype.toString() method can be used to check if a value is object-like in JavaScript.

Example 3

<!doctype html>
<html>
<head>
   <title>Examples</title>
</head>
<body>
   <div id="result1"></div>
   <div id="result2"></div>
   <div id="result3"></div>
   <script>
      document.getElementById("result1").innerHTML = Object.prototype.toString.call({})
      document.getElementById("result2").innerHTML = Object.prototype.toString.call([])
      document.getElementById("result3").innerHTML = Object.prototype.toString.call(function(){})
   </script>
</body>
</html>

In the first statement within the script tag, we are using the Object.prototype.toString() method to convert an empty object {} to a string. The Object.prototype.toString() method returns the string "[object Object]" for an empty object.

In the second statement within the script tag, we are using the Object.prototype.toString() method to convert an empty array [] to a string. The Object.prototype.toString() method returns the string "[object Array]" for an empty array.

In the third statement, we are using the Object.prototype.toString() method to convert an anonymous function function(){} to a string. The Object.prototype.toString() method returns the string "[object Function]" for an anonymous function.

Conclusion

In this tutorial, we looked at three different methods for checking if a value is object-like in JavaScript. We looked at the typeof operator, the instanceof operator, and the Object.prototype.toString() method. All of these methods can be used to check if a value is object-like in JavaScript.

Updated on: 2022-06-24T08:24:55+05:30

143 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements