Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials TypeScript Access Modifiers
TypeScript Intermediate FREE

Access Modifiers

Lesson 3 of 17 Intermediate Interactive

TypeScript supports public, private, and protected access modifiers.

Syntax

TYPESCRIPT
class User {
    public name: string;
    private password: string;
    protected email: string;
}
Access Modifiers
TYPESCRIPT
class BankAccount {
    public owner: string;
    private balance: number;
    protected bank: string;

    constructor(owner: string, balance: number) {
        this.owner = owner;
        this.balance = balance;
        this.bank = "SukhNexus Bank";
    }

    public deposit(amount: number): void {
        this.balance += amount;
    }

    public getBalance(): number {
        return this.balance;
    }
}

let acc = new BankAccount("Alice", 1000);
acc.deposit(500);
console.log(`${acc.owner}: $${acc.getBalance()}`);

Practice

1
Exercise

Practice this concept.

Answer
Write the code as shown above.

Quick Quiz

1

What did you learn?

This covers the basics.

Interview Questions

It is a fundamental TypeScript feature.