πŸ” JavaScript typeof Operator – Know What You're Dealing With

pushpesh kumarpushpesh kumar
3 min read

Ever wanted to know what kind of value a variable holds in JavaScript?

That’s where the typeof operator comes in. It tells you the type of any value or variable.

But... sometimes it lies. πŸ˜…
Let’s break it all down: what typeof gives you, what’s true, what’s weird, and how to handle it.


βœ… The Syntax

typeof value

Returns a string describing the type of the value.


πŸ”’ 1. Numbers

console.log(typeof 42);           // "number"
console.log(typeof 3.14);         // "number"
console.log(typeof NaN);          // "number" ← yes, still a number

βœ… Includes both integers, floats, and NaN.


πŸ”€ 2. Strings

console.log(typeof 'hello');      // "string"
console.log(typeof "");           // "string"
console.log(typeof `template`);   // "string"

βœ… Works with single, double, and backtick quotes.


βš™οΈ 3. Booleans

console.log(typeof true);         // "boolean"
console.log(typeof false);        // "boolean"

βœ… Straightforward.


❓ 4. Undefined

let x;
console.log(typeof x);            // "undefined"
console.log(typeof undefined);    // "undefined"

βœ… Default for uninitialized variables.


🚫 5. Null (🧨 Surprise!)

console.log(typeof null);         // "object"  ❌ (bug in JS since 1995)

🧠 Even though it says "object", null is not an object.

βœ… To detect null properly:

value === null;

πŸ“¦ 6. Objects

console.log(typeof {});           // "object"
console.log(typeof { name: "JS" }); // "object"

βœ… Basic objects return "object".


πŸ“‹ 7. Arrays (still objects!)

console.log(typeof [1, 2, 3]);    // "object" ❌

Array.isArray([1, 2, 3]);         // true βœ…

typeof can't distinguish arrays from objects. Use Array.isArray() instead.


πŸ› οΈ 8. Functions

function greet() {}
const arrow = () => {};

console.log(typeof greet);        // "function"
console.log(typeof arrow);        // "function"

βœ… The only case where typeof returns "function" β€” everything else is "object" or primitives.


πŸ”£ 9. Symbols

const sym = Symbol('id');
console.log(typeof sym);          // "symbol"

βœ… Unique and immutable β€” often used as keys.


πŸ”’ 10. BigInt

const big = 1234567890123456789012345678901234567890n;
console.log(typeof big);          // "bigint"

βœ… For numbers larger than Number.MAX_SAFE_INTEGER.


πŸ”„ Summary Table

Value/Typetypeof result
42, NaN"number"
'hello', ``"string"
true, false"boolean"
undefined"undefined"
null"object" ❌
{}"object"
[]"object" ❌
function() {}"function" βœ…
Symbol('id')"symbol"
123n"bigint"

πŸ§ͺ Bonus: typeof vs constructor vs Array.isArray()

CheckWorks ForUse When
typeofPrimitives, functionsQuick check
Array.isArray(val)ArraysArray detection
val.constructor.nameAny objectGet class name
val instanceof ClassCustom typesInheritance checking

βœ… Final Thoughts

  • Use typeof for primitive types.

  • Use Array.isArray() for arrays.

  • Don't trust typeof null β€” it lies.

  • For more complex type checks, use constructor or instanceof.

0
Subscribe to my newsletter

Read articles from pushpesh kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

pushpesh kumar
pushpesh kumar