JavaScript typeof Operator

The JavaScript typeof operator returns a string describing the type category of a value. It is useful for basic runtime checks, especially for strings, numbers, Booleans, functions, undefined, and BigInt or Symbol values.

Syntax:

typeof value
typeof(value)

Both forms work because typeof is an operator, not a function. Parentheses can still improve readability around a complex expression.

Possible typeof Return Values Top ↑

For ordinary JavaScript values, typeof returns strings such as "undefined", "boolean", "number", "bigint", "string", "symbol", "function", or "object".

console.log(typeof undefined);   // "undefined"
console.log(typeof true);        // "boolean"
console.log(typeof 5.73);        // "number"
console.log(typeof 10n);         // "bigint"
console.log(typeof "plus2net"); // "string"
console.log(typeof Symbol());    // "symbol"
console.log(typeof function () {}); // "function"
console.log(typeof {});          // "object"

Common typeof Examples Top ↑

This modernizes the original page's mixed-value example while keeping the same learning purpose.

class Student {}
const student = new Student();
const students = [];
const active = false;

console.log(typeof student);  // "object"
console.log(typeof students); // "object"
console.log(typeof active);   // "boolean"

Using typeof to Check for undefined Top ↑

typeof is often used when a value may be undefined.

let result;

if (typeof result === "undefined") {
  console.log("No value has been assigned yet");
}

For a genuinely undeclared identifier, typeof missingName also returns "undefined" instead of throwing a ReferenceError in ordinary cases.

Why typeof null Returns object Top ↑

A long-standing JavaScript quirk is that typeof null returns "object". Do not use typeof value === "object" alone when you need to distinguish real objects from null.

const value = null;

console.log(typeof value); // "object"
console.log(value === null); // true

Checking Arrays and Objects Top ↑

Arrays also return "object" with typeof. Use Array.isArray() when you specifically need to know whether a value is an array.

const items = ["HTML", "CSS", "JavaScript"];

console.log(typeof items);        // "object"
console.log(Array.isArray(items)); // true

For class instances, instanceof can sometimes answer a more specific question than typeof, although it has limitations across realms.

typeof NaN and Number Checks Top ↑

NaN means “Not-a-Number”, but its JavaScript type is still number. Use Number.isNaN() when you need to detect the special NaN value.

const result = Number("hello");

console.log(typeof result);       // "number"
console.log(Number.isNaN(result)); // true

Functions and Classes Top ↑

Callable function values return "function". A class declaration itself also reports "function", while an instance created with new is normally an object.

function add(a, b) {
  return a + b;
}

class Course {}

console.log(typeof add);          // "function"
console.log(typeof Course);       // "function"
console.log(typeof new Course()); // "object"

Undeclared Identifiers and the Temporal Dead Zone Top ↑

There is an important exception to the “safe undeclared check” idea. A lexical binding declared with let or const cannot be accessed with typeof before its declaration within the same scope.

// console.log(typeof score); // ReferenceError
let score = 10;
console.log(typeof score); // "number"

See JavaScript variables and the temporal dead zone for the underlying scope rule.

Checking a Value Before and After Conversion Top ↑

The original tutorial used parseFloat() and then checked the result with typeof. That remains a useful demonstration, but remember that a numeric type can still contain NaN.

let value = "5.25";
console.log(typeof value); // "string"

value = parseFloat(value);
console.log(typeof value); // "number"
console.log(Number.isNaN(value)); // false

Read more about parseFloat().

When typeof Is Not Enough Top ↑

  • Use Array.isArray(value) for arrays.
  • Use value === null for null.
  • Use Number.isNaN(value) for NaN.
  • Use instanceof when checking a known constructor relationship is appropriate.
  • For application data, validate the structure and required properties instead of assuming typeof "object" proves the shape is correct.

Common typeof Mistakes Top ↑

  • Writing typeof value === "array"; JavaScript never returns "array".
  • Expecting typeof null to return "null".
  • Assuming every value with type number is a usable finite number.
  • Thinking typeof returns a constructor or class name.
  • Calling typeof a function; it is an operator.

Related pages: variables, isNaN and NaN checks, parseFloat(), and JavaScript structure.




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer