Course contentsShow
← JavaScript
Lesson 745 of 2,72318. ClassesFree lesson

Declaring a Class with the class Keyword

Learn the basic class declaration syntax and how it differs from function declarations.

Declaring a Class with the class Keyword

What you'll learn: How to declare a class using modern JavaScript's class syntax and understand its basic structure.

The Core Idea

In JavaScript, the class keyword provides a cleaner, more structured way to create objects with shared behavior. While classes are syntactic sugar over the prototype system you've already learned, they offer a more familiar and readable syntax that resembles class-based languages.

Basic Class Declaration Syntax

A class declaration looks like this:

class Person {
  // Class body goes here
}

Notice how this differs from declaring a function:

// Function declaration
function createPerson() {
  // ...
}

// Class declaration
class Person {
  // ...
}

The key differences:

  • Capital letter convention: Classes typically start with a capital letter (like Person, not person)
  • No parentheses: Unlike functions, class declarations don't have () after the name
  • Curly braces contain the class body: Methods and other class members go inside {}

Important Distinctions

Classes are NOT hoisted like function declarations:

// This works:
let user = createUser();
function createUser() { }

// This throws an error:
let person = new Person(); // ReferenceError!
class Person { }

You must declare the class before using it.

Key Takeaway: The class keyword provides a cleaner syntax for creating constructor-like structures. Unlike function declarations, classes are not hoisted and must be declared before use. They start with a capital letter and contain their body in curly braces without parentheses after the name.