๐Ÿงฎ JavaScript Variables & Data Types
Estimated reading: 3 minutes 101 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