TypeScript supports public, private, and protected access modifiers.
Getting Started
What is TypeScript?
Basic Types
Types & Interfaces
Async TypeScript
Promises & Async/Await
TypeScript
Intermediate
FREE
Access Modifiers
Lesson 3 of 17
Intermediate
Interactive
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.