Skip to content

PRO TIP Block youtube.com at the DNS level — Pi-hole, NextDNS or your hosts file — but allow youtube-nocookie.com and i.ytimg.com. These tutorials keep playing; the rabbit hole does not.

Learning without distractions

Learn JavaScript STATIC keyword in 8 minutes! ⚡

43.2K views on YouTube

// 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();