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
How to check if a value is object-like in JavaScript?
In JavaScript, an object-like value is any non-primitive value that has a type of "object". This includes objects, arrays, dates, and regular expressions, but excludes primitives like strings, numbers, booleans, null, and undefined. Understanding how to identify object-like values is crucial for data validation and type checking.
What Makes a Value Object-Like?
Object-like values share these characteristics:
- They are not primitive types (string, number, boolean, null, undefined, symbol)
- They can hold properties and methods
- They are reference types, not value types
Method 1: Using the typeof Operator
The typeof operator returns "object" for most object-like values, but has some limitations with null and functions.
<!DOCTYPE html>
<html>
<head>
<title>typeof Operator Example</title>
</head>
<body>
<div id="result"></div>
<script>
function checkObjectLike(value, name) {
return name + ": " + typeof value + " - " + (typeof value === 'object' && value !== null ? "Object-like" : "Not object-like");
}
let results = [
checkObjectLike({}, "Empty object {}"),
checkObjectLike([], "Array []"),
checkObjectLike(null, "null"),
checkObjectLike("hello", "String"),
checkObjectLike(42, "Number")
];
document.getElementById("result").innerHTML = results.join("<br>");
</script>
</body>
</html>
Empty object {}: object - Object-like
Array []: object - Object-like
null: object - Not object-like
String: string - Not object-like
Number: number - Not object-like
Note: typeof null returns "object" due to a JavaScript quirk, so we need additional null checking.
Method 2: Using the instanceof Operator
The instanceof operator checks if a value is an instance of a specific constructor, making it useful for identifying object types.
<!DOCTYPE html>
<html>
<head>
<title>instanceof Operator Example</title>
</head>
<body>
<div id="result"></div>
<script>
function checkInstanceOf(value, name) {
return name + " instanceof Object: " + (value instanceof Object);
}
let results = [
checkInstanceOf({}, "Empty object {}"),
checkInstanceOf([], "Array []"),
checkInstanceOf(new Date(), "Date object"),
checkInstanceOf(function(){}, "Function"),
checkInstanceOf("hello", "String primitive")
];
document.getElementById("result").innerHTML = results.join("<br>");
</script>
</body>
</html>
Empty object {} instanceof Object: true
Array [] instanceof Object: true
Date object instanceof Object: true
Function instanceof Object: true
String primitive instanceof Object: false
Method 3: Using Object.prototype.toString()
The Object.prototype.toString() method provides the most reliable way to identify the exact type of object-like values.
<!DOCTYPE html>
<html>
<head>
<title>toString Method Example</title>
</head>
<body>
<div id="result"></div>
<script>
function getObjectType(value, name) {
return name + ": " + Object.prototype.toString.call(value);
}
let results = [
getObjectType({}, "Empty object"),
getObjectType([], "Array"),
getObjectType(new Date(), "Date"),
getObjectType(/regex/, "RegExp"),
getObjectType(null, "null")
];
document.getElementById("result").innerHTML = results.join("<br>");
</script>
</body>
</html>
Empty object: [object Object] Array: [object Array] Date: [object Date] RegExp: [object RegExp] null: [object Null]
Comparison Table
| Method | Handles null correctly | Identifies specific types | Best for |
|---|---|---|---|
typeof |
No (returns "object") | No | Basic object-like check |
instanceof |
Yes | Yes | Constructor-specific checks |
toString() |
Yes | Yes | Precise type identification |
Complete Object-Like Check Function
<!DOCTYPE html>
<html>
<head>
<title>Complete Object-Like Check</title>
</head>
<body>
<div id="result"></div>
<script>
function isObjectLike(value) {
return typeof value === 'object' && value !== null;
}
// Test various values
let testValues = [
{name: "Object", value: {}},
{name: "Array", value: []},
{name: "null", value: null},
{name: "String", value: "hello"},
{name: "Number", value: 42},
{name: "Date", value: new Date()}
];
let results = testValues.map(test =>
test.name + ": " + isObjectLike(test.value)
);
document.getElementById("result").innerHTML = results.join("<br>");
</script>
</body>
</html>
Object: true Array: true null: false String: false Number: false Date: true
Conclusion
Use typeof value === 'object' && value !== null for a simple object-like check. For more specific type identification, combine with instanceof or Object.prototype.toString.call() depending on your needs.
