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();
-
3:41:41
Barry Cunningham
5 hours agoPRESIDENT TRUMP IS STILL MAGA STRONG! ARE YOU?
80K61 -
28:41
Side Scrollers Podcast
4 hours ago$750 Content Creator Trivia Game Show
17.3K -
1:37:24
Badlands Media
17 hours agoAltered State S3 Ep. 36: Epstein Disillusion, FISA-Gate, and the Grok AI Uprising
53.6K10 -
LIVE
I_Came_With_Fire_Podcast
13 hours agoNO EPSTEIN FILES | AI Marco Rubio | China Attacks Germany | Cloud Seeding in TX & NM?
274 watching -
3:14:05
Geeks + Gamers
4 hours agoGeeks + Gamers Play- MARIO KART WORLD
28.6K -
LIVE
Alex Zedra
3 hours agoLIVE! Scary Games tn!
298 watching -
47:26
Friday Beers
7 hours ago $1.43 earnedWe Drank 12 Beers & Found the Zodiac Killer
32.1K2 -
1:02:33
Mike Rowe
1 day agoCan We Fix The American Diet? | Jillian Michaels #441 | The Way I Heard It
20K17 -
1:08:23
VapinGamers
4 hours ago $0.95 earnedDune Awakening - Community Spice and Everything Nice Game Night - !rumbot !music
15.5K -
3:32:02
Fragniac
5 hours ago🔴 MAKE FPS GREAT AGAIN❗
13.8K