Javascript Objects/Classes - When to use the _ syntax vs. Getter/Setter

Link: https://www.codecademy.com/paths/front-end-engineer-career-path/tracks/fecp-javascript-syntax-part-iii/modules/fecp-learn-javascript-syntax-classes/projects/build-a-library

Hello everyone, I’m on the “Build a library” portion of Javascript Syntax 3 where we are learning about classes. My question is mostly about when to use the _variableName inside of a method versus using the getter or setter method instead. For example, I’ve created a setter method for a boolean isCheckedOut

set isCheckedOut(bool) {
    this._isCheckedOut = bool
}

next, we need to create a ToggleCheckoutStatus method that flips the property value.
Initially, I assume I want to call the setter method for isCheckedOut like this:

this.isCheckedOut(!this._isCheckedOut) // call the setter method this.isCheckedOut passing in the negated property value

this should flip my this._isCheckedOut boolean value.
However, the tip says to instead do this:

this._isCheckedOut = !this._isCheckedOut

which seems much more simple I must say. However, I thought it was bad practice to access properties this way and change them directly no? When is it ok to do this? Would my first version even work?

Something we have to contend with when writing a setter is the parameter. There must be one, and only one parameter. One way of approaching this is to simply assign the parameter to the property, and all is done.

What about when we don’t want a parameter (such as toggle state)? JS makes it easy for us, just don’t use the parameter. Mind, a linter will squawk over the unused variable. We seem to be bound by the core syntax, in essence, albeit we can manipulate and validate and dress up the value being set.

A compromise is to use a helper method to do the toggling.

const book = {
 _isCheckedOut: false,
  get isCheckedOut() {
    return this._isCheckedOut;
  },
  set isCheckedOut(bool) {
    this._isCheckedOut = bool;
  },
  toggleIsCheckedOut() {
    this.isCheckedOut = ! this.isCheckedOut;
  }
}
book.toggleIsCheckedOut()
console.log(book.isCheckedOut)
book.toggleIsCheckedOut()
console.log(book.isCheckedOut)
true
false

Now the setter is working as expected, and we make proper use of both getter and setter.