What is the output of the following code? class Counter { #count = 0; increment() { this.#count++; } get value() { return this.#count; } } const c = new Counter(); c.increment(); console.log(c.value, c.#count);

JavaScript Professional Hard

JavaScript Professional — Hard

What is the output of the following code? class Counter { #count = 0; increment() { this.#count++; } get value() { return this.#count; } } const c = new Counter(); c.increment(); console.log(c.value, c.#count);

Key points

  • Private fields in JavaScript are inaccessible from outside the class
  • The #count field is private in the Counter class
  • Accessing a private field outside the class results in a SyntaxError

Ready to go further?

Related questions