static Keyword in TypeScript

  • static keyword is applied to the fields or methods of a Class.
  • static field retains its value till the program finishes execution.
  • There is no need to create an object to access the data members (fields or methods) of a class. They can be directly accessed by the name of the class. For example, ClassName.DataMember;

Below is an example related to static keyword and how we can use it.

  class Car { 
    static dealer:string;
    model:string;
    static allocationDetails():void{
        console.log("Car is alloted!");
    }     
    carDetails():void{
        console.log(this.model + " is available with " + Car.dealer);
    }
 }
 var myCar = new Car();
 myCar.model = "Hyundai Creta";
 Car.dealer = "ABC Sales";
 myCar.carDetails();
 Car.allocationDetails();

Below is the output of above code

Power Platform Academy

Start or Upgrade your Career with Power Platform

Learn with Akanksha

Python | Azure | AI/ML | OpenAI | MLOps

Design a site like this with WordPress.com
Get started