What Is a Class?
In this lesson, you’ll learn how to create a Class, which is the first step in Object-Oriented Programming.
To define a class in JavaScript, use the class keyword, give the class a name, and place its body inside curly braces ({}).
class Car {}That’s all it takes to create an empty class.
Choosing a Class Name
After the class keyword, you provide a name for the class.
By convention, class names start with a capital letter, following the PascalCase naming style.
Examples include:
CarMyCarCarModelElectricCarYou may choose any valid name as long as it is not a reserved keyword in the language.
Curly Braces
Curly braces ({}) are required when defining a class, even if the class doesn’t contain any code yet.
If you omit them, the JavaScript parser cannot recognize the class definition correctly, resulting in a syntax error.
Key Takeaways
- Use the
classkeyword to define a new class. - By convention, class names use PascalCase.
- A class can have any valid name that is not a reserved keyword.
- Curly braces (
{}) are always required, even for an empty class.