Static Fields and Static Methods

It is possible to create static fields or methods in JavaScript classes.

class Math {
  static PI = 3.14;

  static max(...args) { 
    let max = Number.MIN_SAFE_INTEGER;
    args.forEach( arg => max = max < arg ? arg : max );
    return max;
  }
}

console.log(Math.PI);
console.log(Math.max(3, 0, 5));

Static fields/methods in JavaScript, like in Java/C++, are class members and not instance members. So, you access/invoke them using the class name (not the instantiated object).

Aside: A class is technically a function! A function is technically an object, so a class is an object!! You can add value/function properties outside of the class definition. Those additions will be counted as static members.

class Person {
  constructor(name) {
    this.name = name;
  }
}

Person.genus = "homosapien";

const person = new Person("Ali");

console.log(person);
console.log(Person.genus)