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();
-
9:16
Zoufry
21 hours ago $0.03 earnedThe Evil Design of Mcdonald's Drive Thru
165 -
LIVE
GritsGG
1 hour agoWe Are Winning Every Game!!!! Most Wins 3040+! 🔥
53 watching -
1:57:28
Gary Cardone
1 day ago $0.33 earnedLIVE With Gary Cardone
1.77K2 -
8:01
MattMorseTV
18 hours ago $10.81 earnedTrump just DROPPED a BOMBSHELL.
66.4K83 -
LIVE
FyrBorne
11 hours ago🔴Warzone M&K Sniping: Is The Overlook OP or Bait?
350 watching -
2:00:53
MG Show
21 hours agoTrump Stops the Epstein Nonsense & Exposes FAKE MAGA; Victory Lap
94.6K91 -
39:21
MattGaetz
1 day agoThe Anchormen Show with Matt Gaetz | Feeding the Dragon
34.8K28 -
6:01
Michael Button
2 days ago $3.64 earnedMy History Degree Lied About Lost Civilizations
46.6K16 -
9:36
MattMorseTV
1 day ago $5.71 earnedThe EU just FOUND OUT.
31.9K51 -
22:39
Forrest Galante
1 day agoPrivate Tour of Australia's Craziest Zoo
167K15