Are there really no private variables in JavaScript?

Private variables are variables that are visible only to the class to which they belong. It is a common opinion that there are no strict private variables in JS (except for the new fields like this.#property).
Can anyone kindly explain why the variable ‘boy’ bellow cannot be considered to be a private one?

class Forrest {
  constructor() {
    let boy = "Bobby"
    this.getBoy = function() {
      console.log(boy)
    }
  }
}

const f = new Forrest()
f.getBoy() // Bobby

11 thoughts on “Are there really no private variables in JavaScript?”

Leave a Comment