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
