Tutorials Exercises Services Get Certified Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++
Learn
more
Learn
more
Replay
JavaScript Classes
‹ Previous Next ›
ECMAScript 2015, also known as ES6, introduced JavaScript Classes.
JavaScript Classes are templates for JavaScript Objects.
JavaScript Class Syntax
Use the keyword class to create a class.
Always add a method named constructor() :
Syntax
class ClassName {
constructor() { ... }
}
Example
class Car {
constructor(name, year) {
https://www.w3schools.com/js/js_class_intro.asp 02/11/2024, 10 48 AM
Page 1 of 8
:
this.name = name;
this.year = year;
}
}
The example above creates a class named "Car".
The class has two initial properties: "name" and "year".
A JavaScript class is not an object.
It is a template for JavaScript objects.
Using a Class
When you have a class, you can use the class to create objects:
Example
const myCar1 = new Car("Ford", 2014);
const myCar2 = new Car("Audi", 2019);
Try it Yourself »
The example above uses the Car class to create two Car objects.
The constructor method is called automatically when a new object is created.
The Constructor Method
The constructor method is a special method:
https://www.w3schools.com/js/js_class_intro.asp 02/11/2024, 10 48 AM
Page 2 of 8
:
It has to have the exact name "constructor"
It is executed automatically when a new object is created
It is used to initialize object properties
If you do not define a constructor method, JavaScript will add an empty constructor
method.
ADVERTISEMENT
Class Methods
Class methods are created with the same syntax as object methods.
Use the keyword class to create a class.
Always add a constructor() method.
Then add any number of methods.
Syntax
class ClassName {
constructor() { ... }
method_1() { ... }
method_2() { ... }
method_3() { ... }
}
https://www.w3schools.com/js/js_class_intro.asp 02/11/2024, 10 48 AM
Page 3 of 8
:
Create a Class method named "age", that returns the Car age:
Example
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
age() {
const date = new Date();
return date.getFullYear() - this.year;
}
}
const myCar = new Car("Ford", 2014);
document.getElementById("demo").innerHTML =
"My car is " + myCar.age() + " years old.";
Try it Yourself »
You can send parameters to Class methods:
Example
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
age(x) {
return x - this.year;
}
}
const date = new Date();
let year = date.getFullYear();
const myCar = new Car("Ford", 2014);
document.getElementById("demo").innerHTML=
https://www.w3schools.com/js/js_class_intro.asp 02/11/2024, 10 48 AM
Page 4 of 8
:
"My car is " + myCar.age(year) + " years old.";
Try it Yourself »
Browser Support
The following table defines the first browser version with full support for Classes in
JavaScript:
Chrome 49 Edge 12 Firefox 45 Safari 9 Opera 36
Mar, 2016 Jul, 2015 Mar, 2016 Oct, 2015 Mar, 2016
"use strict"
The syntax in classes must be written in "strict mode".
You will get an error if you do not follow the "strict mode" rules.
Example
In "strict mode" you will get an error if you use a variable without declaring it:
class Car {
constructor(name, year) {
this.name = name;
this.year = year;
}
age() {
// date = new Date(); // This will not work
const date = new Date(); // This will work
return date.getFullYear() - this.year;
https://www.w3schools.com/js/js_class_intro.asp 02/11/2024, 10 48 AM
Page 5 of 8
:
}
}
Try it Yourself »
Learn more about "strict mode" in: JS Strict Mode.
‹ Previous Next ›
W3schools Pathfinder
Track your progress - it's free! Sign Up Log in
ADVERTISEMENT
COLOR PICKER
https://www.w3schools.com/js/js_class_intro.asp 02/11/2024, 10 48 AM
Page 6 of 8
:
ADVERTISEMENT
PLUS SPACES GET CERTIFIED
FOR TEACHERS FOR BUSINESS CONTACT US
Top Tutorials Top References
HTML Tutorial HTML Reference
CSS Tutorial CSS Reference
JavaScript Tutorial JavaScript Reference
How To Tutorial SQL Reference
SQL Tutorial Python Reference
Python Tutorial W3.CSS Reference
W3.CSS Tutorial Bootstrap Reference
Bootstrap Tutorial PHP Reference
PHP Tutorial HTML Colors
Java Tutorial Java Reference
C++ Tutorial Angular Reference
jQuery Tutorial jQuery Reference
Top Examples Get Certified
HTML Examples HTML Certificate
https://www.w3schools.com/js/js_class_intro.asp 02/11/2024, 10 48 AM
Page 7 of 8
:
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate
FORUM ABOUT ACADEMY
W3Schools is optimized for learning and training. Examples might be simplified to
improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our terms
of use, cookie and privacy policy.
Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by
W3.CSS.
https://www.w3schools.com/js/js_class_intro.asp 02/11/2024, 10 48 AM
Page 8 of 8
: