๐Ÿงฎ JavaScript Variables & Data Types
Estimated reading: 3 minutes 338 views

JavaScript typeof โ€“ Identify Data Types Like a Pro

Ever tried debugging a JavaScript variable and wondered what type it is? Thatโ€™s where the typeof operator becomes your best friend.


Why typeof Matters

The typeof operator is essential for:

  • Debugging variable types
  • Writing safe conditional logic
  • Handling dynamic values in APIs or forms

Whether you’re a beginner or working with complex applications, understanding typeof helps you avoid logic errors and type mismatches.


What Youโ€™ll Learn

By the end of this article, you’ll be able to:

  • Use typeof to detect types of variables
  • Understand quirks and edge cases (like typeof null)
  • Apply typeof in real-world checks

Core Concept of typeof

The typeof operator returns a string indicating the type of the unevaluated operand.

Syntax:

typeof operand

OR

typeof(operand)

Both formats are valid. The operand can be any variable, value, or expression.


JavaScript typeof in Action

Here are practical examples with full explanations:


Example 1: Basic Primitive Types

console.log(typeof "W3Office");   // string
console.log(typeof 42); // number
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
console.log(typeof Symbol()); // symbol

Explanation:

  • "W3Office" โ†’ returns "string"
  • 42 โ†’ returns "number"
  • true โ†’ returns "boolean"
  • undefined โ†’ returns "undefined"
  • Symbol() โ†’ returns "symbol"

Example 2: Special Case โ€“ null

console.log(typeof null); // object

Explanation:

  • typeof null returns "object" โ€” this is a well-known JavaScript bug for historical reasons.
  • Always verify null explicitly using:
value === null

Example 3: Complex Types (Objects & Functions)

console.log(typeof {});               // object
console.log(typeof []); // object
console.log(typeof function(){}); // function

Explanation:

  • {} โ†’ generic object โ†’ "object"
  • [] โ†’ array โ†’ also "object" (use Array.isArray() to confirm array)
  • function(){} โ†’ function โ†’ "function"

Example 4: Type Checking Before Operation

function W3OfficeCheckType(input) {
if (typeof input === "number") {
return input * 2;
}
return "Not a number!";
}

console.log(W3OfficeCheckType(10)); // 20
console.log(W3OfficeCheckType("Hello")); // Not a number!

Explanation:

  • This safely doubles the number if it is a number.
  • Otherwise, it returns a message.

Advanced Techniques & Best Practices

Best Practice: Use typeof for Safe Checks

if (typeof userData !== "undefined") {
console.log("User data exists!");
}

Avoid ReferenceError for undeclared variables.


Common Pitfall: Arrays and null

Valuetypeof ResultProper Check
[]"object"Array.isArray(value)
null"object"value === null

Use precise methods to check array or null instead of relying only on typeof.


Summary & Key Takeaways

  • typeof is a unary operator that returns the data type as a string
  • Use it to guard logic, prevent type mismatches, and debug values
  • Watch out for quirks like typeof null and arrays being "object"
  • Use Array.isArray() or strict equality for better type detection

FAQ โ€“ JavaScript typeof

What does typeof null return in JavaScript?

typeof null returns "object" due to a historical bug in JavaScript.

Can I use typeof on undeclared variables?

Yes. typeof undeclaredVar will return "undefined" and wonโ€™t throw an error.

How do I check if a value is an array using typeof?

You can’t. Use Array.isArray(value) instead of typeof.

Whatโ€™s the difference between typeof and instanceof?

typeof returns a string type, while instanceof checks inheritance from constructors.

Is typeof an operator or a function?

Itโ€™s an operator, though it can be used with or without parentheses.

Share Now :
Share

JavaScript Typeof

Or Copy Link

CONTENTS
Scroll to Top