Skip to content

Variable Declaration

Modern JavaScript/TypeScript should never use var because:

  • var is function-scoped rather than block-scoped, leading to unexpected behavior
  • var allows redeclaration of the same variable in the same scope
  • var variables are hoisted, which can create confusion
  • let and 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;
    }
}

✅ DO

let count = 5; // For variables that need to be reassigned
const userId = 42; // For variables that shouldn't change

class User {
    name: string;
    email: string;

    hello(): string {
        return "Hello " + this.name;
    }
}