Premium Only Content

Learn JavaScript STATIC keyword in 8 minutes! ⚡
// static = keyword that defines properties or methods that belong
// to a class itself rather than the objects created
// from that class (class owns anything static, not the objects)
// ----------- EXAMPLE 1 -----------
class MathUtil{
static PI = 3.14159;
static getDiameter(radius){
return radius * 2;
}
static getCircumference(radius){
return 2 * this.PI * radius;
}
static getArea(radius){
return this.PI * radius * radius;
}
}
console.log(MathUtil.PI);
console.log(MathUtil.getDiameter(10));
console.log(MathUtil.getCircumference(10));
console.log(MathUtil.getArea(10));
// ----------- EXAMPLE 2 -----------
class User{
static userCount = 0;
constructor(username){
this.username = username;
User.userCount++;
}
static getUserCount(){
console.log(`There are ${User.userCount} users online`);
}
sayHello(){
console.log(`Hello, my username is ${this.username}`);
}
}
const user1 = new User("Spongebob");
const user2 = new User("Patrick");
const user3 = new User("Sandy");
user1.sayHello();
user2.sayHello();
user3.sayHello();
User.getUserCount();
-
2:38:42
Barry Cunningham
7 hours agoIS PRESIDENT TRUMP DIRECTING A MOVIE...OR IS IT CHESS? AND MORE NEWS!
13K23 -
LIVE
Ian427
39 minutes agoIrritating Ian is LIVE! MarioKart Night with SMAY!
136 watching -
LIVE
Rance's Gaming Corner
46 minutes ago"Blue Shells, Broken Bonds, and Big Laughs 🎮💣" Mario Kart 8 Deluxe
34 watching -
58:49
Tactical Advisor
7 hours agoUnboxing Tacpack/New 1854 Lever Action | Vault Room Live Stream 029
98.8K8 -
15:19
T-SPLY
21 hours agoMayor Busted: Nashville Mayor Not Concerned Over Leaking Federal Agents Information!
43.4K108 -
14:05
Mrgunsngear
8 hours ago $3.32 earnedHolosun 507 Comp Review: The Best Pistol Optic?
31.6K8 -
LIVE
GGrikki
2 hours ago18+ Just a girl trying to survive. SCUM
71 watching -
LIVE
Pepkilla
2 hours agoPlaying Some Warzone and Super People Today ~
69 watching -
SpartakusLIVE
3 hours agoBorderlands Fan Fest || #Borderlands4 #2KPartner - !BL4
147K1 -
2:52:40
The Quartering
6 hours agoSaturday Play Through! Clair Obscur: Expedition 33
49.9K7