Do you know what will be the output of this code?
JavaScript
class Account {
#balance = 100;
get balance() {
return this.#balance;
}
}
const acc = new Account();
acc.balance = 500;
console.log(acc.balance);
Show answer and explanation
The answer is 100.
balance has a getter but no setter, so the assignment has no effect. In strict mode (like ES modules) it throws a TypeError instead. #balance is a private field that can’t be reached from outside.