Variable Declaration¶
Modern JavaScript/TypeScript should never use var because:
varis function-scoped rather than block-scoped, leading to unexpected behaviorvarallows redeclaration of the same variable in the same scopevarvariables are hoisted, which can create confusionletand const provide block scoping and better predictability
In TypeScript classes, public is indeed the default visibility and generally unnecessary. The only time public might add
value is when you're being explicit for documentation purposes or to contrast with other modifiers in the same class,
but it's technically redundant.
🚨 DON’T¶
var count = 5;
var userId = 42;
class User {
public name: string;
public email: string;
public hello(): string {
return "Hello " + this.name;
}
}