Open Playground
JS ObjectsLesson #237

JavaScript Static Methods

Static class methods are defined on the class itself.

Static class methods are defined on the class itself.

You cannot call a static method on an object, only on an object class.

Example

Example Code

javascript
Try it Yourself
class Car {
  constructor(name) {
    this.name =
  name;

  }
  static hello() {
    return "Hello!!";

  }
}

const myCar = new Car("Ford");

// You can call 'hello()' on
  the Car Class:
document.getElementById("demo").innerHTML
  = Car.hello();

// But NOT on a Car Object:
// document.getElementById("demo").innerHTML
  = myCar.hello();
// this will raise an error.

If you want to use the myCar object inside the static method, you can send it as a parameter:

Example

Example Code

javascript
Try it Yourself
class Car {
  constructor(name) {
    this.name =
  name;

  }
  static hello(x) {
    return "Hello " +
  x.name;

  }
}
const myCar = new Car("Ford");
document.getElementById("demo").innerHTML
  = Car.hello(myCar);


Interactive Code Examples

JS Example 1

javascript
Try it Yourself
Interactive runnable JavaScript example from EA Academy.
Live Output Preview:

JS Example 2

javascript
Try it Yourself
Interactive runnable JavaScript example from EA Academy.
Live Output Preview: