The typeof Operator on Classes
You'll learn why typeof reports classes as "function" rather than "class", revealing their true nature.
What typeof Tells Us About Classes
When you use the typeof operator on a class, JavaScript returns "function", not "class". This might seem surprising at first, but it perfectly reveals what we learned earlier: classes are syntactic sugar over constructor functions.
Here's what happens:
class Dog {
constructor(name) {
this.name = name;
}
}
console.log(typeof Dog); // "function"
Even though we wrote class Dog, JavaScript treats it as a function under the hood. This is because classes are constructor functions with a cleaner syntax.
Why This Makes Sense
Remember that classes were added to JavaScript as a more elegant way to write prototype-based inheritance. The underlying mechanism didn't change—it's still functions and prototypes. When you declare a class, JavaScript creates a function that can be called with new, just like the old constructor function pattern.
This is identical to:
function Dog(name) {
this.name = name;
}
console.log(typeof Dog); // "function"
Both produce "function" because both are functions at their core.
The Takeaway
Using typeof on a class reveals that classes aren't a fundamentally new type in JavaScript—they're constructor functions dressed in modern syntax. The prototype-based nature of JavaScript hasn't changed; classes just make it more approachable.
Key Takeaway: The typeof operator returns "function" for classes because classes are constructor functions with syntactic sugar—JavaScript's prototype system remains underneath.