Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Class static Keyword

JavaScript Class static Keyword

JavaScript Class (class) JavaScript Classes (class)


Example

Example

The following example creates a class "Runoob" and a static method hello():

class

Runoob

{

constructor

(

name

)

{

this

.

name

=

name

;

}

static

hello

(

)

{

return

"

Hello!!

"

;

}

}

let

noob

=

new

Runoob

(

"

Runoob Tutorial

"

)

;

//

The 'hello()' method can be called on the class

document

.

getElementById

(

"

demo

"

)

.

innerHTML

=

Runoob

.

hello

(

)

;

//

A static method cannot be called through an instantiated object

//

document.getElementById("demo").innerHTML = noob.hello();

//

The above code will raise an error

Try it »


Definition and Usage

A class (class) defines static methods through the static keyword.

Static methods are called directly on the class, not on instances of the class.

Static methods are often used to create utility functions.

Syntax

static methodName()

Technical Details

JavaScript version: ECMAScript 2015 (ES6)

Browser support

static is an ECMAScript6 (ES6) feature.

ES6 (JavaScript 2015) is supported by all major browsers today.

Chrome Edge Firefox Safari Opera
Yes Yes Yes Yes Yes

Internet Explorer 11 or older IE does not support the static keyword.


JavaScript Class (class) JavaScript Classes (class)