static
Marks a field or method as belonging to the class itself, not to instances created with new.
Example
#class Student {
static count = 0
constructor(name) {
this.name = name
Student.count++
}
static compare(a, b) {
return a.name.localeCompare(b.name, 'en')
}
}
const students = [new Student('Mia'), new Student('Emma'), new Student('Noah')]
console.log(Student.count)
console.log(students.toSorted(Student.compare).map((student) => student.name))
console.log(students[0].count)The instance doesn’t have a count field. It belongs to the class, so you can only read it through Student.count.
Definition and usage
#The static keyword in front of a field or method makes it belong to the class, not to its instances. You reach it through the class name, the way you do with Math.max() or Array.from(), without creating an object. Instances can’t access static members through this.
Static methods are a good place for functions related to the class that don’t need a particular object’s data: factory methods that create instances in a specific way (Course.fromJSON()), validators, or functions that compare two instances. Static fields hold data shared by the whole class, such as a count of created objects or configuration constants.
Inside a static method, this refers to the class, so you can refer to other static members. They’re inherited by derived classes. A static { } block runs its code once, while the class is being defined, for example to set up a more complex static field.
Syntax
#class Name {
static field = value
static #privateField = value
static method() {}
static {
// code that runs once, when the class is defined
}
}
Name.method()
Name.fieldMore examples
#class Course {
constructor(title, lessons) {
this.title = title
this.lessons = lessons
}
static fromJSON(json) {
const data = JSON.parse(json)
return new Course(data.title, data.lessons)
}
}
const course = Course.fromJSON('{"title":"React","lessons":16}')
console.log(course)
console.log(course instanceof Course)class Character {
static create(name) {
return new this(name)
}
constructor(name) {
this.name = name
}
}
class Guide extends Character {}
console.log(Character.create('Astro'))
console.log(Guide.create('Ra'))The Guide class inherited the create() method. Called as Guide.create(), this equals Guide, so it creates an instance of the right class.
class Levels {
static thresholds = []
static {
for (let level = 1; level <= 5; level++) {
Levels.thresholds.push(level * level * 100)
}
}
}
console.log(Levels.thresholds)Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- If a method doesn’t use any instance data, consider whether it should be static, or just a regular function outside the class.
- In a constructor or instance method, refer to a static field through the class name, such as
Student.count. Writingthis.countlooks for a field on the object, not on the class. - Pass a static method that uses
thisas a callback carefully. Detached from the class, it losesthis, just like regular methods do.
Related entries
#- classDefines a class: a template for creating objects that share the same fields and methods.
- extendsCreates a derived class that inherits another class’s methods and can extend or override them.
- superRefers to the base class: calls its constructor or its version of a method.
- #fieldFields and methods prefixed with # are private: only code inside the class can access them.
- thisA keyword pointing to the object a function was called on.
Spotted a mistake or missing an example? Write to us.