Difference between public, and having no access modifier on a TypeScript variable

Question:

I have the following examples:

Without public;

 export class Persona {
   name: string;
   lastName: string;
   dni: string;
}

With public;

 export class Persona {
   public name: string;
   public lastName: string;
   public dni: string;
}

If I leave the attribute without the access modifier, is it the public default? Possibly it is something basic but I don't know what the correct definition would be (I want the attributes to be public to access them from the html in angular 5).

Answer:

In Typescript there are 3 access modifiers: public, private and protected ( See reference , in English, for more details)

  • public : It is the default modifier. This is so because Javascript does not have these modifiers and everything is public, so for consistency it is normal for it to be the default value.

  • private : The elements (attributes and methods) are only visible within the class.

  • protected : Elements are only visible within the class and in classes that inherit directly from it.

Scroll to Top