Member-only story

Choosing Between Class Expressions and Class Declarations in JavaScript

Understand the differences and pick the right tool for your next project

Max N
2 min readApr 3, 2024

With the growing popularity of object-oriented programming in JavaScript, many developers have embraced ES6 classes as a means to create concise yet expressive code structures. While most tutorials cover basic class definitions, few delve into subtle distinctions between class expressions and declarations — two similar but distinct approaches available to programmers.

This post aims to clarify these nuances, helping you make informed decisions regarding their appropriate application.

Class Declarations

Much like traditional function declarations, class declarations offer named entities scoped to their respective enclosing environments. These entities remain hoisted, making them accessible anywhere within their scope. Additionally, they contribute to the global namespace pollution should no local identifier shadow them.

Here’s an example showcasing a typical class declaration:

class Vehicle {
constructor(make, model) {
this.make = make;
this.model = model;
}

startEngine() {
console.log('Vroom!');
}
}

const car = new Vehicle('Toyota'…

--

--

Max N
Max N

Written by Max N

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.

No responses yet