November 8, 2023
isObject utility helps to clarify this by providing a robust way to check if a value is an object type, excluding null.
How to Implement
To create an
typeof operator considers anything that's not a primitive type (string, number, boolean, undefined, symbol, BigInt) as an object. Here's a simple implementation:
isObject, you can pass any value to it to check if that value is an object:
Handling Special Cases
null is considered an object when using
isObject function correctly identifies
null as not an object.
Arrays and Functions
isObject to return
false for these types, you'll need to modify the implementation:
isObject with Constructor Functions
It's important to remember that objects created through constructor functions are also objects:
isObject can check for object types, remember that it doesn't differentiate between plain objects and object instances created from classes or constructor functions. Use
instanceof if you need to distinguish between instances and plain objects.
How to Truncate Date in MySQL