Class Basics in JavaScript

From the JavaScript cheat sheet · Classes · verified Jul 2026

Class Basics

Creating and using classes

javascript
// Class declaration
class Person {
  constructor(name) {
    this.name = name;
  }
  
  greet() {
    return `Hello, I'm ${this.name}`;
  }
}

const john = new Person('John');
john.greet();
💡 Classes are syntactic sugar over prototypes
⚡ Class methods are non-enumerable by default
📌 Class declarations are not hoisted
🔥 Always use new with classes, calling without throws error

Continue with JavaScript

Save the full cheat sheet or work through every related task.

More JavaScript tasks

Back to the full JavaScript cheat sheet