Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to create Static Variables in JavaScript ?
Next article icon

JavaScript Static Methods

Last Updated : 20 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Static methods are functions that are defined on a class but are not accessible through instances of the class. Instead, they are called directly on the class itself. These methods are useful for creating utility functions or shared logic that doesn’t depend on individual object instances.

Syntax

class ClassName {
static methodName() {
// method logic
}
}
JavaScript
class MathUtils {     static add(a, b) {         return a + b;     }      static multiply(a, b) {         return a * b;     } }  // Calling static methods on the class console.log(MathUtils.add(5, 3)); console.log(MathUtils.multiply(4, 6)); 

Output
8 24 

In this example, the add and multiply methods can be called directly on the MathUtils class, without creating an instance.

Characteristics of Static Methods

  1. Class-Based: Static methods are invoked on the class itself, not on instances of the class.
  2. No Access to Instance Properties: Static methods do not have access to ‘this’ or instance-specific properties.
  3. Common Use Cases:
    • Utility functions (e.g., mathematical operations, string manipulations).
    • Factory methods for object creation.
    • Shared logic that doesn’t depend on individual objects.

Use case of Static Methods

1. Static Method Math.max()

The getMax() method is a static method that wraps the built-in Math.max() function, demonstrating how static methods can be used to perform operations directly on the class.

JavaScript
class Calc {     static getMax(...numbers) {         return Math.max(...numbers);     } } console.log(Calc.getMax(1, 5, 3, 9)); 

Output
9 

The getMax() method takes any number of arguments and returns the highest value using the Math.max() function.

2. Static Method Array.isArray()

Static methods like checkArray() allow you to check for specific conditions (e.g., checking if a variable is an array) without needing to instantiate the class.

JavaScript
class Validator {     static check(input) {         return Array.isArray(input);     } } console.log(Validator.check([1, 2, 3])); console.log(Validator.check('hello')); 

The checkArray() method uses the Array.isArray() method to determine if the input is an array.

3. Static Method Counter

Static methods can be used to manage class-level state, such as a counter, without creating individual instances.

JavaScript
class Count {     static c = 0;     static inc() {         return ++Count.c;     }          static reset() {         Count.c = 0;     } } console.log(Count.inc());  console.log(Count.inc());  Count.reset(); console.log(Count.inc());  

Output
1 2 1 

The Count class has a static property c, which keeps track of the number of times the inc() method is called. The reset() method allows resetting the counter.

4. Static Method Factory Pattern

Static methods can serve as factory methods to create instances of a class, simplifying object creation and initialization.

JavaScript
class User {     constructor(name, age) {         this.name = name;         this.age = age;     }      static create(name, age) {         return new User(name, age);     } } const user = User.create('Ajay', 30); console.log(user); 

The createUser() static method is a factory method that returns a new instance of the User class.

5. Static Method Singleton Pattern

Static methods can be used to implement design patterns like the Singleton, where only one instance of the class is allowed.

JavaScript
class DB{     static instance     constructor()     {         if(DB.instance)         {             return DB.instance         }         DB.instance=this     }   static  getinstance()     {         if(!DB.instance)         {             DB.instance=new DB()         }         return DB.instance     } } const obj1=new DB() const obj2=new DB() console.log(obj1===obj2) 

Output
true 

In the DB class, the getInstance() method ensures that only one instance of the class is created. If an instance already exists, it returns the existing one. This is an implementation of the Singleton pattern, ensuring a single point of access to the class.



Next Article
How to create Static Variables in JavaScript ?
author
abhishekg25
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • javascript-oop

Similar Reads

  • JS Static Methods
    Static methods are functions that are defined on a class but are not accessible through instances of the class. Instead, they are called directly on the class itself. These methods are useful for creating utility functions or shared logic that doesn’t depend on individual object instances. Syntax cl
    3 min read
  • How to create Static Variables in JavaScript ?
    To create a static variable in JavaScript, you can use a closure or a function scope to encapsulate the variable within a function. This way, the variable maintains its state across multiple invocations of the function. Static keyword in JavaScript: The static keyword is used to define a static meth
    2 min read
  • Static Function in PHP
    In certain cases, it is very handy to access methods and properties in terms of a class rather than an object. This can be done with the help of static keyword. Any method declared as static is accessible without the creation of an object. Static functions are associated with the class, not an insta
    3 min read
  • Comparison between static and instance method in PHP
    Static methods The static method in PHP is same as other OOP languages. Static method should be used only when particular data remains constant for the whole class. As an example, consider that some programmer is making the data of a college and in that every object needs getCollegeName function tha
    3 min read
  • When to use static vs instantiated classes in PHP?
    Prerequisite - Static Function PHP In PHP, we can have both static as well as non-static (instantiated) classes. Static class Introduction: A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member (variable) or a static member function (m
    5 min read
  • Static Method in Java With Examples
    In Java, the static keyword is used to create methods that belongs to the class rather than any specific instance of the class. Any method that uses the static keyword is referred to as a static method. Features of Static Method: A static method in Java is associated with the class, not with any obj
    3 min read
  • Static Method vs Instance Method in Java
    In Java, methods are mainly divided into two parts based on how they are associated with a class, which are the static method and the Instance method. The main difference between static and instance methods is: Static method: This method belongs to the class and can be called without creating an obj
    4 min read
  • Difference between static and non-static method in Java
    A static method is a method that belongs to a class, but it does not belong to an instance of that class and this method can be called without the instance or object of that class. Every method in java defaults to a non-static method without static keyword preceding it. Non-static methods can access
    6 min read
  • Static method in Interface in Java
    Static Methods in Interface are those methods, which are defined in the interface with the keyword static. Unlike other methods in Interface, these static methods contain the complete definition of the function and since the definition is complete and the method is static, therefore these methods ca
    2 min read
  • Static Variables in Java
    In Java, when a variable is declared with the static keyword. Then, a single variable is created and shared among all the objects at the class level. Static variables are, essentially, global variables. All instances of the class share the same static variable. These are the main scenarios when we u
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences