---
title: "What is class in JavaScript full explain?"  
description: "What is class in JavaScript full explain?"  
author: "Ravi Vishwakarma"  
published: 2021-07-19  
canonical: https://answers.mindstick.com/qa/93750/what-is-class-in-javascript-full-explain  
category: "web application"  
tags: ["javascript", "web application development", "web development"]  
reading_time: 5 minutes  

---

# What is class in JavaScript full explain?

What is [class](https://yourviews.mindstick.com/view/81038/business-class-of-israel-raged-with-coronavirus-pandemic) in [JavaScript](https://www.mindstick.com/articles/1530/design-a-simple-stylish-calculator-using-html-css-and-javascript) [full](https://www.mindstick.com/articles/12893/experience-the-full-power-of-suitecrm) [explain](https://yourviews.mindstick.com/view/86025/content-created-for-humans-not-for-bots-explain-this-statement)?

## Answers

### Answer by user

Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on prototypes. Classes are in fact 'special functions', and just as you can define function expressions and function declarations.

```
Syntax class class-name [ extends other class] {  //public or private fields  // constructor  //get and set property  //function }
```

**There are two way defining the class.** **Class declarations** One way to define a class is using a class declaration. To declare a class, you use the class keyword with the name of the class ('Rectangle' here).

```
class Rectangle {  constructor(height, width) {    this.height = height;    this.width = width;  }}
```

**Class expressions** A class expression is another way to define a class. Class expressions can be named or unnamed. The name given to a named class expression is local to the class's body. However, it can be accessed via the name property.

```
// unnamed class let Rectangle = class {  constructor(height, width) {    this.height = height;    this.width = width;  }};console.log(Rectangle.name);
```

**Constructor** The constructor method is a special method for creating and initializing an object created with a class. There can only be one special method with the name 'constructor' in a class. A SyntaxError will be thrown if the class contains more than one occurrence of a constructor method. A constructor can use the super keyword to call the constructor of the super class.

```
class Rectangle {  constructor(height, width) {    this.height = height;    this.width = width;  }  // Getter property  get area() {    return this.calcArea();  }  // Method  calcArea() {    return this.height * this.width;  }}const square = new Rectangle(10, 10);console.log(square.area);
```

**Field declarations** **Public field declarations** With the JavaScript field declaration syntax, the above example can be written as:

```
class Rectangle {  height = 0;  width;  constructor(height, width) {    this.height = height;    this.width = width;  }}
```

**Private field declarations**

```
Using private fields, the definition can be refined as below.class Rectangle {  #height = 0;  #width;  constructor(height, width) {    this.#height = height;    this.#width = width;  }}
```

**Extend the super class** The extends keyword is used in class declarations or class expressions to create a class as a child of another class.

```
class Animal {  constructor(name) {    this.name = name;  }  speak() {    console.log(`${this.name} makes a noise.`);  }}class Cat extends Animal {  constructor(name) {    super(name); // call the super class constructor and pass in the name parameter  }  speak() {    console.log(`${this.name} barks.`);  }}let d = new Cat('Meow');d.speak(); 
```

\

### Answer by Ethan Karla

Class and Object is a very important part for OOP. The concept of OOP is dependent on Objects and Classes. There can be many member functions and data members inside the class. Which is accessed through the object. Class is like a structure. In which variable(data members) and function(member functions) are write at one place. Data member and member functions are the members of the class. Works to hold class data. Class This is the layout of the object.

```
Syntax
 class class-name [ extends other class] {  //public or private fields  // constructor  //get and set property  //function }
```

## Class declarations

we are declaring a class using the class keyword with the name of the class 'Rectangle' in JavaScript.

```
class Rectangle {  constructor(height, width) {    this.height = height;    this.width = width;
}
```

## Class expressions

A class expression is another way to declare a class. Class expressions can be named or unnamed class declarations. This class will access by temporary name.

temporary_name.function-name() and temporary_name.fields-name

```
// unnamed class
let Rectangle = class {  constructor(height, width) {    this.height = height;    this.width = width;  }};
```

## Constructor

Constructor is a special method for creating and initializing a variable created with a class in JavaScript. There is only one constructor method in JavaScript, A SyntaxError will be throw if the class contains more than one constructor method. A constructor can use the super keyword to call the constructor of the superclass.

```
class Rectangle {  constructor(height, width) {    this.height = height;    this.width = width;  }  // Getter property  get area() {    return this.calcArea();  }  // Method  calcArea() {    return this.height * this.width;  }}const square = new Rectangle(10, 10);console.log(square.area); // 100
```

## Field declarations

## Public field declarations

```
class Rectangle {  height = 0; width;  constructor(height, width) {
    this.height = height;    this.width = width;  }}
```

## Private field declarations

```
class Rectangle {  #height = 0;  #width;  constructor(height, width) {    this.#height = height;    this.#width = width;  }}
```

## Extends the superclass

The extends keyword is used in class to create a class as a child of a parent class.

```
//Parent Class declarationclass Animal {
  constructor(name) {
    this.name = name;
  }
  speak() {
    console.log(`${this.name} makes a noise.`);
  }
}
```

```
//Child Class declarationclass Dog extends Animal {
  constructor(name) {
    super(name); // call the super class constructor and pass in the name parameter
  }
  speak() {
    console.log(`${this.name} barks.`);
  }
}
let d = new Dog('Mitzie');
d.speak(); // Mitzie barks.
```

\


---

Original Source: https://answers.mindstick.com/qa/93750/what-is-class-in-javascript-full-explain

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
