Skip to content
Advertisement

Is a JavaScript class an object?

I assume that an ES6 class is an object as “everything” is objects in JavaScript. Is that a correct assumption?

Advertisement

Answer

From the point of view of Object Oriented Programming class is not an object. It is an abstraction. And every object of that is a concrete instance of that abstraction.

From the point of view of JavaScript, class is an object, because class is a ES6 feature and under it simple function is used. It is not just an abstraction in Javascript, but also an object by itself. And that function is an object. It has it’s own properties and functions.

So speaking in Javascript not everything is an object. There are also primitive types – number, string, boolean, undefined, symbol from ES6. When you will use some methods with this primitive types except undefined, they will be converted into the objects.

You can see the below example.

const str = 'Text';
const strObj = new String('Text');

console.log(str);
console.log(strObj.toString());

console.log(typeof str);
console.log(typeof strObj);

There is also one extra primitive type null, but checking it’s type returns an object. This is a bug.

console.log(typeof null);
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement