Class declarations and Class expressions are different ways of defining classes in JavaScript.
Class declarations
We use the keyword class to declare a class. Just like this:
class Person {
constructor(name, birthDate) {
this.name = name;
this.birthDate = birthDate;
}
}
Function declarations are hoisted. Class declarations are NOT hoisted. That means we need to declare the class before trying to access it or we’ll get a ReferenceError.
Class expressions
Class expressions can be named or without a name.
Named Class expressions Example
// named
let Person = class PersonClassNamed {
constructor(name, birthDate) {
this.name = name;
this.birthDate = birthDate;
}
};
console.log(Person.name);
// output: "PersonClassNamed"
Unnamed Class expressions Example
// without a name
let Person = class {
constructor(name, birthDate) {
this.name = name;
this.birthDate = birthDate;
}
};
console.log(Person.name);
// output: "Person"
The hoisting restrictions applies to Class expressions.
MDN has a good documentation on this.
class declaration Expressions Hoisting