JavaScript Tutorial

Javascript Classes

A class in JavaScript is like a blueprint or a template that helps us create objects with similar properties and behaviors. It's a way to organize and structure our code.

In JavaScript, classes are often referred to as "syntactic sugar" because they provide a more convenient syntax for working with objects and prototypes. It means that classes in JavaScript are a more readable and intuitive way to write code, but they are ultimately based on the existing prototype-based inheritance model of the language.

The class syntax contains two components:-

  • Class declarations
  • Class expressions

Class Declaration

A class in Javascript is defined by using a class declaration. A class keyword is used to declare a class with any particular name. According to JavaScript naming conventions, the name of the class always starts with an uppercase letter.

Input:-

Output:-

Class Expression

A class expression doesn’t require an identifier after the class keyword. And we can use a class expression in a variable declaration and pass it into a function as an argument. For example, the following defines a class expression:-

Input:-

Output:-

Go back to Previous Course