JavaScript
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Types
-
Section 3: Strings and Numbers in Depth
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Objects and Arrays
-
Section 7: Maps, Sets, and Symbols
-
Section 8: Asynchronous JavaScript
-
Section 9: Object-Oriented and Prototypes
-
Section 10: The DOM
-
Section 11: Browser APIs
-
Section 12: Modern JavaScript (ES2015-ES2025)
-
Section 13: Functional Programming Patterns
-
Section 14: Error Handling and Debugging
-
Section 15: Testing
-
Section 16: Accessibility for JavaScript Developers
-
Section 17: Internationalization and Localization
-
Section 18: Performance
-
Section 19: Node.js Fundamentals
-
Section 20: Regular Expressions
-
Section 21: Design Patterns in JavaScript
-
Section 22: Security Basics
-
Section 23: Data Structures and Algorithms in JavaScript
-
Section 24: Practical Projects
-
Section 25: More Advanced Async Patterns
-
Section 26: More Object and Class Practice
-
Section 27: Working with Dates and Internationalization
-
Section 28: Web Components
-
Section 29: More DOM and Browser Practice
-
Section 30: Build Tooling for Vanilla JavaScript
-
Section 31: More Practice Projects
-
Section 32: Interview and Algorithm Practice
-
Section 33: Error Objects (MDN Reference)
-
Section 34: TypedArrays and Binary Data
-
Section 35: Reflection and Metaprogramming (MDN Reference)
-
Section 36: More Global Functions (MDN Reference)
15: typeof and instanceof
I remember early in my career, I was building a data-processing pipeline for a client. I had a function that expected an array of user IDs, and to be "safe," I added a check: if (typeof ids === 'object'). It seemed logical—arrays are objects, right? Everything worked perfectly in testing. Then, the first time we hit production, the API occasionally returned null when no users were found. Because typeof null is actually 'object' in JavaScript (a legacy bug that can never be fixed without breaking the internet), my code tried to run a .forEach() on null and crashed the entire process. I spent three hours staring at a stack trace before I realized my type-checking was lying to me.
The Limits of typeof
The typeof operator is your go-to for checking primitive values. If you need to know if a variable is a string, a number, or a boolean, it's fast and reliable. I use it all the time for simple input validation. For example, if you're writing a function that formats a price, you'll want to make sure the input is actually a number before you try to call .toFixed(2) on it.
const price = 19.99;
if (typeof price === 'number') {
console.log(price.toFixed(2));
}
But as I learned the hard way, typeof is a blunt instrument. It doesn't distinguish between different types of objects. Whether you have a plain object, an array, a Date object, or null, typeof will tell you it's an 'object'. This is where you'll find yourself reaching for something more surgical.
Precision Checking with instanceof
When you need to know exactly what kind of object you're dealing with, instanceof is the tool for the job. Instead of returning a string, instanceof returns a boolean by checking if the constructor of the object exists anywhere in the object's prototype chain. I find this incredibly useful when dealing with custom classes or built-in complex objects like Dates or Arrays.
If we go back to my payment pipeline disaster, instanceof Array would have saved me. null instanceof Array is false, whereas typeof null === 'object' is true. Here is how that distinction looks in practice:
const users = ['Alice', 'Bob'];
const emptyResponse = null;
console.log(users instanceof Array); // true
console.log(emptyResponse instanceof Array); // false
const now = new Date();
console.log(now instanceof Date); // true
console.log(typeof now); // 'object' (Not very helpful!)
One thing to keep in mind: instanceof is specifically for objects. If you try to use it on a primitive string or number, it will return false because those aren't instances of the Object constructor in the way you'd expect. I usually follow a simple rule of thumb: use typeof for primitives and instanceof for anything created with new or built-in object types.
📋 Practical Task
Build a Mixed-Type Data Filter
Imagine you are receiving a "dirty" stream of data from a legacy API. The data arrives as an array containing a mix of strings, numbers, dates, and plain objects. Your goal is to write a function called sortDataByType that organizes this mess into a categorized object.
Requirements:
- Create a function
sortDataByType(inputArray). - The function should return an object with four keys:
strings,numbers,dates, andothers. - Use
typeofto identify strings and numbers. - Use
instanceofto identifyDateobjects. - Anything that doesn't fit those three categories should go into
others.
Test your function with this array:
const rawData = ["Hello", 42, new Date(), { id: 1 }, "World", 100, new Date(), null, [1, 2, 3]];
// Expected result:
// {
// strings: ["Hello", "World"],
// numbers: [42, 100],
// dates: [Date object, Date object],
// others: [{ id: 1 }, null, [1, 2, 3]]
// }There are no comments for now.