The static keyword is basically used to create a static property or method of the class. Javascript static methods and properties of a class can’t be called through a class instance. It is called directly from the class.
A Javascript static method can be created by using static
keyword followed by static method name and then curly braces {}
. Write static method statements inside the curly braces.
Source Code
class Person {
static personInfo() {
document.write('I am a static method');
}
}
Person.personInfo();
Static property can be created using static
keyword followed by the variable name.
Source Code
class Person {
static workinField = "developer";
}
document.write(Person.workinField);