typeof & Type Checking
EasyType checking in JavaScript requires understanding multiple tools because no single operator covers all cases. typeof works for primitives but has quirks (typeof null === "object"). Array.isArray() is needed for arrays, instanceof checks prototype chains, and custom type guards handle complex cases.
Interactive Visualization
String Immutability
Step 1: Create
let str = "hello";
Output
"hello"
1 / 4
Key Points
- typeof returns a string: "string", "number", "boolean", "undefined", "object", "function", "symbol", "bigint"
- typeof null === "object" is a famous bug that can never be fixed
- typeof for undeclared variables returns "undefined" (does not throw)
- Array.isArray() is the only reliable way to check for arrays
- instanceof checks the prototype chain (works for custom classes)
- Combine multiple checks for robust type validation
Code Examples
typeof Results Table
console.log(typeof "hello"); // "string" console.log(typeof 42); // "number" console.log(typeof true); // "boolean" console.log(typeof undefined); // "undefined" console.log(typeof Symbol()); // "symbol" console.log(typeof {}); // "object" console.log(typeof []); // "object" (not "array"!) console.log(typeof function(){}); // "function" console.log(typeof null); // "object" (!)
typeof is useful for primitives but returns "object" for null, arrays, and most objects.
Array.isArray — The Only Reliable Array Check
const arr = [1, 2, 3]; const obj = { 0: "a", 1: "b", length: 2 }; console.log(typeof arr); // "object" console.log(typeof obj); // "object" console.log(Array.isArray(arr)); // true console.log(Array.isArray(obj)); // false // instanceof fails across iframes console.log(arr instanceof Array); // true // But: iframe.contentWindow.Array !== window.Array
Array.isArray() is the only reliable check. instanceof Array fails across different realms (iframes).
typeof for Safe Property Access
// typeof does NOT throw for undeclared variables console.log(typeof undeclaredVar); // "undefined" (no error!) // Practical: feature detection if (typeof window !== "undefined") { console.log("Running in browser"); } else { console.log("Running in Node.js"); } // Checking for null/undefined function isNullish(value) { return value == null; // loose equality trick } console.log(isNullish(null)); // true console.log(isNullish(undefined)); // true console.log(isNullish(0)); // false
typeof is safe for undeclared variables. Use it for feature detection and environment checks.
Comprehensive Type Checking
function getType(value) { if (value === null) return "null"; if (Array.isArray(value)) return "array"; return typeof value; } console.log(getType(null)); // "null" console.log(getType([1, 2])); // "array" console.log(getType({ a: 1 })); // "object" console.log(getType("hello")); // "string" // Object.prototype.toString — most precise function preciseType(value) { return Object.prototype.toString.call(value); } console.log(preciseType([])); // "[object Array]" console.log(preciseType(null)); // "[object Null]" console.log(preciseType(new Date())); // "[object Date]"
Combine typeof, Array.isArray, and null checks for coverage. Object.prototype.toString is the most precise.
Common Mistakes
- Relying on typeof to detect null (typeof null === "object")
- Using typeof to check for arrays (returns "object", not "array")
- Using instanceof Array instead of Array.isArray (fails across iframes)
- Forgetting that typeof returns "function" for functions, not "object"
Interview Tips
- Know the typeof null bug — it is a legacy issue from JS v1
- Always recommend Array.isArray over instanceof for arrays
- Show the complete typeof results table from memory
- Demonstrate Object.prototype.toString.call() as the ultimate type checker