How to Get JavaScript Object Type

Finding the type of a JavaScript value starts with deciding what your code needs to distinguish. JavaScript type checks report broad categories and can test a value against a specific built-in or constructor.

This article compares typeof with object tags and shows how to check for arrays explicitly.

TL;DR

Use the check that matches the question: typeof for primitive categories, Array.isArray() for arrays, Object.prototype.toString.call() for built-in tags, and instanceof for a known constructor in the same JavaScript context.

  • typeof null is “object”, so test null with value === null.
  • constructor.name is descriptive, but it is not a safe universal type test.
  • Built-in tags can be changed through Symbol.toStringTag.

What Is JavaScript Object Type Checking?

A primitive is a value such as a number or string. Arrays, dates and regular expressions are objects with properties and identity.

The typeof operator returns a short category, including “function” for callable values. Arrays and dates both appear as “object”, so that result alone cannot select their specific behavior.

An input validator may need to accept arrays but reject a plain record, while a logger may want a built-in tag.

How to Check an Object Type Step by Step

Use the example to compare JavaScript’s broad operator with built-in tags and array detection.

Step 1: Compare typeof with an object tag

Save the following as object_type_demo.js and run node object_type_demo.js. The tag call gives familiar labels for built-ins, while the custom class shares the plain object’s tag.

const values = {
  plain: { id: 7 },
  array: [1, 2],
  date: new Date('2026-01-02T00:00:00Z'),
  nullValue: null,
  regex: /js/i,
  custom: new (class Widget {})(),
};
for (const [key, value] of Object.entries(values)) {
  const tag = Object.prototype.toString.call(value);
  console.log(`${key}: typeof=${typeof value}, tag=${tag}`);
}
console.log(`array check: ${Array.isArray(values.array)}`);
console.log(`constructor name: ${values.custom.constructor.name}`);

The output from node object_type_demo.js is shown below.

plain: typeof=object, tag=[object Object]
array: typeof=object, tag=[object Array]
date: typeof=object, tag=[object Date]
nullValue: typeof=object, tag=[object Null]
regex: typeof=object, tag=[object RegExp]
custom: typeof=object, tag=[object Object]
array check: true
constructor name: Widget
Node.js output comparing JavaScript typeof, object tags, array detection and a custom constructor

I ran this comparison and saw the array, date and null all produce typeof=object, while their tags differ. The custom Widget’s constructor name prints as Widget.

Step 2: Detect arrays explicitly

Array.isArray(value) returns a boolean for array values.

Array.isArray([1, 2]); // true
Array.isArray({ 0: 1, length: 1 }); // false

Use the result before calling array methods or reading indexes.

Step 3: Check a known class with instanceof

instanceof checks whether a constructor appears in the value’s prototype chain.

const createdAt = new Date();
console.log(createdAt instanceof Date);
console.log(createdAt instanceof RegExp);

Run node instanceof_demo.js for the Date comparison:

true
false
Node.js output showing instanceof Date as true and instanceof RegExp as false

A Date from another frame can produce false for instanceof Date because each frame has its own constructor.

Step 4: Use typeof for primitive checks

I tested an undeclared name with typeof and got “undefined” without an ordinary-read ReferenceError.

typeof "hello"; // "string"
typeof 42; // "number"
typeof (() => {}); // "function"
typeof missingName; // "undefined"

Edge Cases and Limits

These cases show where a type result can mislead and which check gives the useful answer.

CaseWhat happensUse instead
nulltypeof returns “object”value === null
Arraytypeof returns “object”Array.isArray(value)
Known constructorinstanceof follows the prototype chainUse in the same JavaScript context
Custom classObject tag is generally “[object Object]”Use an explicit class check or application discriminator
Symbol.toStringTag setObject.prototype.toString can report the supplied tagTreat the tag as descriptive, not a security check
Undeclared identifiertypeof name returns “undefined”For declared let or const, initialize before access

Symbol.toStringTag lets an object provide its own label. The tag is descriptive, so avoid using it as a security check.

The constructor property may be changed, inherited, or absent. Minification can rename a class, which makes constructor.name useful for diagnostics only.

I evaluated missingValue in Node.js and got ReferenceError: missingValue is not defined. Using typeof missingValue returns “undefined”.

For input validation, test the properties and operations the function needs, such as an own id property.

Object conversion can affect checks that expect an object. Object(value) wraps primitives such as strings and numbers, while Object(null) and Object(undefined) produce empty objects. That conversion changes the value being inspected, so test the original input first when nullish values have a distinct meaning.

An object created with Object.create(null) has no prototype and no inherited constructor property. A direct value.constructor.name check therefore fails for it.

Object static methods answer focused questions about a value’s own data. Object.hasOwn(value, “id”) checks an own property without following the prototype chain. Object.keys(value) lists enumerable own string keys.

The Object description is a broad label for a reference value, not a reliable class name. A plain object, a date, and a regular expression all satisfy typeof value === “object”. An array also has that result, which is why Array.isArray() deserves its own check.

When code needs a particular operation, checking that operation’s preconditions says more than assigning a general label.

Objects made with Object.create(null) are useful as dictionaries when inherited property names should be absent. Their prototype is null, so they do not inherit methods such as hasOwnProperty(), and they have no inherited constructor.

Use Object.hasOwn(record, key) to test a key on supported runtimes rather than calling record.hasOwnProperty(key). This keeps the check working even when the record itself has that key.

Object(value) performs conversion, so it should not be used as a type detector on the original input. It returns an object wrapper for a primitive such as a number or string, but returns an object unchanged.

It also converts null and undefined to empty objects. A check after conversion therefore answers what the converted result is, not what the caller supplied.

Preserve the original value when null or undefined must remain a distinct case.

Instance properties can answer a more useful question than the constructor name. An application may distinguish a message by an own kind property or validate that an input has the fields its next operation reads.

Check own properties when inherited values should not count. If data comes from outside the program, validate its shape and values before relying on them.

A label like “object” does not prove that an expected field exists or has the right value.

Conclusion

Choose the check based on the operation the code will perform.

For related examples, see JavaScript Date Objects and JavaScript null and undefined. MDN documents typeof, Array.isArray() and instanceof.

FAQ

These answers cover common distinctions between JavaScript’s runtime checks.

Why does typeof return object for null?

typeof null returns object for historical compatibility. Check null with value === null.

How do I check whether a value is an array?

Use Array.isArray(value) detects arrays, including arrays from another JavaScript context.

Can constructor.name identify every JavaScript object?

No. The constructor property can be changed or inherited, and minification can rename constructors. Use a check suited to the decision.

What does Object.prototype.toString.call return?

It returns a bracketed tag such as [object Date] or [object Array], but Symbol.toStringTag can customize the tag.

Aditya Gupta
Aditya Gupta

Aditya Gupta is a founding member and editor at CodeForGeek. He first found his way into tech by reading articles, and now writes approachable guides to Node.js security, authentication, AI tools, coding agents, and web scraping.

Articles: 529