static

Marks a field or method as belonging to the class itself, not to instances created with new.

On this page

Example

#
JavaScript
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)
Console
3
['Emma', 'Mia', 'Noah']
undefined

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

#
Syntax
class Name {
  static field = value
  static #privateField = value

  static method() {}

  static {
    // code that runs once, when the class is defined
  }
}

Name.method()
Name.field

More examples

#
A factory method
JavaScript
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)
Console
Course { title: 'React', lessons: 16 }
true
this in a static method, and inheritance
JavaScript
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'))
Console
Character { name: 'Astro' }
Guide { name: 'Ra' }

The Guide class inherited the create() method. Called as Guide.create(), this equals Guide, so it creates an instance of the right class.

A static block
JavaScript
class Levels {
  static thresholds = []

  static {
    for (let level = 1; level <= 5; level++) {
      Levels.thresholds.push(level * level * 100)
    }
  }
}

console.log(Levels.thresholds)
Console
[100, 400, 900, 1600, 2500]

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. Writing this.count looks for a field on the object, not on the class.
  • Pass a static method that uses this as a callback carefully. Detached from the class, it loses this, just like regular methods do.

Related entries

#

Spotted a mistake or missing an example? Write to us.