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();
-
5:03:10
Pepkilla
8 hours agoTime to Party Verdansk
19.1K4 -
4:55:48
iCheapshot
5 hours ago $5.90 earnedWhere We Landing Boys! | Warzone Verdansk is Back Yeerrr
50.9K3 -
1:11:15
Savanah Hernandez
7 hours agoIt’s Time To Start Asking Hard Questions: Where Are The Arrests?
59.2K115 -
2:57:33
TimcastIRL
6 hours agoTrump Tariffs Spark Global PANIC, Countries BEG Trump, CAVE To His Demands | Timcast IRL
211K189 -
1:37:16
Roseanne Barr
8 hours ago $29.99 earnedThe Man Who Saved America | The Roseanne Barr Podcast #93
89.7K49 -
10:49:37
Dr Disrespect
15 hours ago🔴LIVE - DR DISRESPECT - WARZONE - OG VERDANSK
231K29 -
16:38
T-SPLY
11 hours agoDemocrats Are At President Trump For "Liberation Day"
30.6K23 -
2:53:20
vivafrei
11 hours agoModerating Debate on Syria - Roy Gutman vs. Scott Horton - Regime Change Good or Bad?
173K13 -
1:04:37
Man in America
11 hours agoCHEMTRAIL WARFARE: Tom Renz Exposes the Military’s SECRET Chemical Attacks on Americans
37.8K25 -
2:09:33
Precision Rifle Network
1 day agoS4E11 Guns & Grub Live - Playing World Of Tanks!
24.1K3