Static
keyword is used access the properties and functions directly wihout create an object
For example, class has a function which can be called using object(new operator of a class)
class Employee {
getSalary() {
console.log("Salary Details");
}
}
const employe = new Employee();
employe.getSalary(); // Salary Details
if you try to access function with class name it throws TypeError {} is not a function
Employee.getSalary(); // TypeError: Employee.getSalary() is not a function
using static
keyword, we can directly calls function using class names
class Employee {
static getDepartment() {
return "HR"
}
}
console.log(Employee.getDepartment());
static
keyword also applies to functions.