How to Make Class Fields Private in JavaScript?

Find out how to create private JavaScript class fields

Starting with ES13/ES2022, you can specify private class fields/members by prefixing the field name with # (hash symbol).

For example:

// ES13+
class Foo {
    #privateProp;

    constructor() {
        this.#privateProp = 123;
    }
}

const foo = new Foo();

// SyntaxError
console.log(foo.#privateProp);

Similarly, you can:


Hope you found this post useful. It was published . Please show your love and support by sharing this post.