JavaScript class

Example

Create a Car class, and then an object (myCar) based on the Car class:

// Create a Car class
class Car {
constructor(brand) {
    this.carname = brand;
  }
}

// Create a Car Object
myCar = new Car("Ford");

Try it Yourself »


Description

A class is a type of object template.

The class statement initiates a JavaScript class.

Properties and methods are assigned in the constructor() method.

The constructor() method is called each time a class object is initialized.

See Also:

JavaScript Classes.

Notes

The syntax in a class must be written in "strict mode".

Unlike functions, class declarations are not hoisted (you must declare a class before you can use it).


Syntax

class className {
  // class body
}

Related Pages

JavaScript Tutorial: JavaScript Classes

JavaScript Tutorial: JavaScript ES6 (EcmaScript 2015)

JavaScript Tutorial: JavaScript this

JavaScript Tutorial: JavaScript Strict Mode


Browser Support

class is an ECMAScript6 (ES6) feature.

ES6 (JavaScript 2015) is supported in all modern browsers since June 2017:

Chrome 51 Edge 15 Firefox 54 Safari 10 Opera 38
May 2016 Apr 2017 Jun 2017 Sep 2016 Jun 2016

class is not supported in Internet Explorer.


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.