---
title: "What is role of static method in JS?"  
description: "What is role of static method in JS?"  
author: "Ravi Vishwakarma"  
published: 2021-07-19  
canonical: https://answers.mindstick.com/qa/93756/what-is-role-of-static-method-in-js  
category: "web application"  
tags: ["javascript", "web application development", "web development"]  
reading_time: 2 minutes  

---

# What is role of static method in JS?

What is [role](https://yourviews.mindstick.com/view/85446/national-doctor-s-day-recognizing-the-indispensable-role-of-doctors-in-one-s-life) of [static method](https://www.mindstick.com/blog/440/creating-static-methods-in-c-sharp) in JS?

## Answers

### Answer by user

By definition, [static](https://www.mindstick.com/blog/153/static-class-in-c-sharp) methods are associated with a class, not the instances of that class. Thus, static methods are useful for defining helper or utility methods.

```
Syntax:
class class-name{
 constructor(){
  //statement
 }
 static method-name( [arguments] ){
  //statement
 }
}
```

```
Example
class Person {
 constructor(name) {
  this.name = name;
 }
 getName() {
  return this.name;
 }
 static createAnonymous(gender) {
  let name = gender == 'male' ? 'John Doe' : 'Jane Doe';
  return new Person(name);
 }
}
```

let anonymous = Person.createAnonymous('male'); If you attempt to call the static [method](https://www.mindstick.com/articles/23411/the-most-effective-method-to-find-the-perfect-small-business-phone-system-for-your-business) from an instance of the class, you’ll get an error. For example. • JavaScript static methods are shared among instances of a class. Thus, they are associated with the class, not any particular instance of that class. • The static methods are called via the class name, not the instances of the class. • Use the className.staticMethodName() or this.constructor.staticMethodName() to call a static method in a class constructor or an instance method. \

### Answer by Ethan Karla

the static keyword defines the static method and property of a class without static function or property is called instance method or property of the class, a static property is useful for the cache. if we create the global variable, This will be there as long as the program is running.

```
Syntax
class class-name{
 constructor(){
  //statement
 }static propertyName [= value]; static method-name( [arguments] ){
  //statement
 }
}
```

## Example

```
<!DOCTYPE html>
<html>
<style>
 p{
    font-size:25px;
    }
</style>
<body>
<h2>My Heading</h2>
<p id='demo'></p>
<script>
class Person {
 constructor(name) {
  this.name = name;
 }
 //normal function
 getName() {
  return this.name;
 }
 //static function
 static createAnonymous(org) {
  let name = org == 'org' ? 'MindStick Software pvt ltd' : 'Uttam Misra';
  return new Person(name);
 }
}
let anonymous = Person.createAnonymous('org');
document.getElementById('demo').innerHTML = anonymous.name;
</script>
</body>
</html>    
```

## Output

> ## My Heading
>
> ## MindStick Software pvt ltd

\


---

Original Source: https://answers.mindstick.com/qa/93756/what-is-role-of-static-method-in-js

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
