Last Updated : 15 Jul, 2025
JavaScript class is a type of function declared with a class keyword, that is used to implement an object-oriented paradigm. Constructors are used to initialize the attributes of a class.
There are 2 ways to create a class in JavaScript.
In this article, we will discuss class expression to declare classes in JavaScript and how to use them.
Class ExpressionThe class expression is another way of creating classes in JavaScript and they can be named or unnamed. If named, the class name is used internally, but not outside of the class.
Syntax
const variable_name = new Class_name {
// class body
}
const variable_name = class{
//class body
}
Example 1: Named class expression
JavaScript
const Website = class Geek {
constructor(name) {
this.name = name;
}
websiteName() {
return this.name;
}
};
const x = new Website("GeeksforGeeks");
console.log(x.websiteName());
Example 2: Unnamed class expression.
JavaScript
const Website = class {
constructor(name) {
this.name = name;
}
returnName() {
return this.name;
}
};
console.log(new Website("GeeksforGeeks").returnName());
Key Features of Class Expressions
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4