/
...
/
/
πŸŽ‰
Static Functions in Classes
Search
Try Notion
πŸŽ‰πŸŽ‰
Static Functions in Classes
Static functions can be created using static keywords inside the class. Now let’s understand why and how static is used!
Why? β†’
suppose you don’t want to create an object from the class for certain reasons and still want that the function should be inside of the class then static is useful
How? β†’
First, create a class, contractor and static function.
class Students { constructor(name, age) { this.name = name; this.age = age; } static add(a, b){ return a + b; } }
JavaScript
Now as we know that we need to create an object first to use the functions and logic inside of the class, but for static, we don’t need to create any object.
console.log(Students.add(1+2)); //Output -> 3
JavaScript