Skip to content
Advertisement

Difference between this.variable and this._variable in JavaScript?

I’m learning the Codecademy Advanced JavaScript course, I’m learning about classes.

In their code, I feel this.variable and this._variable have no different but they require to write this.variable even though 2 options return the same output.

Can you please explain the differences if you understand this?

These are some examples of code

class Dog {
  constructor(name) {
    this._name = name
  }
}

Is it different from:

class Dog {
  constructor(name) {
    this.name = name
  }
}

Advertisement

Answer

**

1.Names can contain letters, digits, underscores, and dollar signs. 2.Names must begin with a letter Names can also begin with $ and _ 3.Names are case sensitive(y and Y are different variables)

**

Advertisement