JavaScript Classes (class)
A class is a template for creating objects.
We use the class keyword to create a class. The class body is inside a pair of curly braces {}, where we can define class members such as methods or constructors.
Each class contains a special method, constructor(), which is the class's constructor. This method is used to create and initialize an object created by class the class.
The syntax for creating a class is as follows:
class
ClassName
{
constructor
(
)
{
...
}
}
Example:
Example
class
Runoob
{
constructor
(
name
,
url
)
{
this
.
name
=
name
;
this
.
url
=
url
;
}
}
The example above creates a class named "Runoob".
Two properties are initialized in the class: "name" and "url".
Browser support
The numbers in the table indicate the first browser version that supports this property.
| Chrome 49 | Edge 12 | Firefox 45 | Safari 9 | Opera 36 |
| Mar, 2016 | Jul, 2015 | Mar, 2016 | Oct, 2015 | Mar, 2016 |
Using a Class
Once the class is defined, we can use the new keyword to create an object:
Example
class
Runoob
{
constructor
(
name
,
url
)
{
this
.
name
=
name
;
this
.
url
=
url
;
}
}
let
site
=
new
Runoob
(
"
Runoob Tutorial
"
,
"
https://www.runoob.com
"
)
;
Try it »
When an object is created, the constructor method constructor() is called automatically.
Class Expressions
A class expression is another way to define a class. Class expressions can be named or unnamed. The name of a named class expression is local to the class body.
Example
//
Unnamed/Anonymous Class
let
Runoob
=
class
{
constructor
(
name
,
url
)
{
this
.
name
=
name
;
this
.
url
=
url
;
}
}
;
console
.
log
(
Runoob
.
name
)
;
//
output: "Runoob"
//
Named Class
let
Runoob
=
class
Runoob2
{
constructor
(
name
,
url
)
{
this
.
name
=
name
;
this
.
url
=
url
;
}
}
;
console
.
log
(
Runoob
.
name
)
;
//
Output: "Runoob2"
Constructor
A constructor method is a special method:
- The constructor method is named constructor().
- The constructor method is executed automatically when a new object is created.
- The constructor method is used to initialize object properties.
- If no constructor method is defined, JavaScript automatically adds an empty constructor method.
Class methods
We use the keyword class to create a class; we can add a constructor() method, and then add any number of methods.
class
ClassName
{
constructor
(
)
{
...
}
method_1
(
)
{
...
}
method_2
(
)
{
...
}
method_3
(
)
{
...
}
}
In the following example, we create an "age" method that returns the age of the website:
Example
class
Runoob
{
constructor
(
name
,
year
)
{
this
.
name
=
name
;
this
.
year
=
year
;
}
age
(
)
{
let
date
=
new
Date
(
)
;
return
date
.
getFullYear
(
)
this
.
year
;
}
}
let
runoob
=
new
Runoob
(
"
Runoob Tutorial
"
,
2018
)
;
document
.
getElementById
(
"
demo
"
)
.
innerHTML
=
"
Runoob Tutorial
"
runoob
.
age
(
)
"
years old.
"
;
Try it »
We can also pass parameters to a class method:
Example
class
Runoob
{
constructor
(
name
,
year
)
{
this
.
name
=
name
;
this
.
year
=
year
;
}
age
(
x
)
{
return
x
this
.
year
;
}
}
let
date
=
new
Date
(
)
;
let
year
=
date
.
getFullYear
(
)
;
let
runoob
=
new
Runoob
(
"
Runoob Tutorial
"
,
2020
)
;
document
.
getElementById
(
"
demo
"
)
.
innerHTML
=
"
Runoob Tutorial
"
runoob
.
age
(
year
)
"
years old.
"
;
Try it »
Strict Mode "use strict"
The bodies of class declarations and class expressions execute in strict mode. For example, constructors, static methods, prototype methods, getters, and setters all execute in strict mode.
If you do not follow strict mode, an error will occur:
Example
class
Runoob
{
constructor
(
name
,
year
)
{
this
.
name
=
name
;
this
.
year
=
year
;
}
age
(
)
{
//
date = new Date(); // error
let
date
=
new
Date
(
)
;
//
Correct
return
date
.
getFullYear
(
)
this
.
year
;
}
}
Try it »
The example in the figure below uses an undeclared variable in a class:

For more on strict mode, see:JavaScript Strict Mode (use strict)
Reference
Class Methods
| Method | Description |
|---|---|
| constructor() | Constructor, used to create and initialize the class |
Class Keyword
| The keyword | Description |
|---|---|
| extends | Inherit from a Class |
| static | Define a Static Method in a Class |
| super | Call the Parent Class's Constructor |