Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Public Members | Access Modifiers in TypeScript
TypeScript Classes and OOP

bookPublic Members

In TypeScript, the public access modifier is used to specify that a class property or method can be accessed from anywhere: inside the class, by instances of the class, and even from code outside the class. In fact, if you do not explicitly specify an access modifier for a class member, TypeScript treats it as public by default. This means your class members are open for access unless you intentionally restrict them.

12345678910111213
class Car { public make: string; public model: string; public start(): void { console.log(`${this.make} ${this.model} is starting.`); } } const myCar = new Car(); myCar.make = "Toyota"; myCar.model = "Corolla"; myCar.start(); // Output: Toyota Corolla is starting.
copy

Using public members is useful when you want to allow other parts of your code to interact freely with your class. For example, if you are building a data model where all properties should be accessible and modifiable, declaring them as public makes your intent clear and keeps your code straightforward. However, you should use public members thoughtfully to avoid exposing implementation details that might be better kept private or protected in future code changes.

1. What is the default access modifier for class members in TypeScript if none is specified?

2. Fill in the blank to declare a public property in a class:

question mark

What is the default access modifier for class members in TypeScript if none is specified?

Select the correct answer

question-icon

Fill in the blank to declare a public property in a class:

name: string;

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 5

bookPublic Members

Swipe to show menu

In TypeScript, the public access modifier is used to specify that a class property or method can be accessed from anywhere: inside the class, by instances of the class, and even from code outside the class. In fact, if you do not explicitly specify an access modifier for a class member, TypeScript treats it as public by default. This means your class members are open for access unless you intentionally restrict them.

12345678910111213
class Car { public make: string; public model: string; public start(): void { console.log(`${this.make} ${this.model} is starting.`); } } const myCar = new Car(); myCar.make = "Toyota"; myCar.model = "Corolla"; myCar.start(); // Output: Toyota Corolla is starting.
copy

Using public members is useful when you want to allow other parts of your code to interact freely with your class. For example, if you are building a data model where all properties should be accessible and modifiable, declaring them as public makes your intent clear and keeps your code straightforward. However, you should use public members thoughtfully to avoid exposing implementation details that might be better kept private or protected in future code changes.

1. What is the default access modifier for class members in TypeScript if none is specified?

2. Fill in the blank to declare a public property in a class:

question mark

What is the default access modifier for class members in TypeScript if none is specified?

Select the correct answer

question-icon

Fill in the blank to declare a public property in a class:

name: string;

Click or drag`n`drop items and fill in the blanks

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
some-alt