Javascript - Class Class Declarations

Introduction

There are two ways in which you can define classes in ES6:Class Declarations and Class Expressions

In class declarations you declare a class using the "class" keyword followed by a class-name.

The name of a class behaves as if it is a constant inside the class definition, which means you cannot overwrite a class using the class name inside itself:

class SimpleClass { 
    constructor() { 
        SimpleClass = "42";      // throws an error during execution 
    } 
} 

SimpleClass = "42";              // works fine after the declaration 

class declarations are not hoisted.

Similar to let and const, class declarations reside in the temporal dead zone (TDZ) until the execution reaches the point of class declaration.

You need to declare your class before accessing it, otherwise a ReferenceError will occur:

const b = new Bike(); // ReferenceError 
class Bike {} 

In the above example, since the class has not already been declared, it exists in the TDZ.

When you try to initialize a new instance using the new keyword, a reference error is thrown.