I think I'm not getting setters and getters. Javascript

So, I’m doing right now the project “Team Stats” of “Introduction to JavaScript”, of Codeacademy, and everything’s working fine, but here’s my question, because I think I’m not getting this:

Why can I push something on an array where I’ve only put a getter and not a setter? I though setter where for that, for modifiyng things.

So, here’s my code

const team = {
  _players: [
     {
    firstName: 'Pablo',
    lastName: 'Sanchez',
    age: 11
    }
  ],
  _games : [
    {
    opponent: 'Broncos',
    teamPoints: 42,
    opponentPoints: 27
      }
  ],
  get players() {
    return this._players
  },
  get games (){
    return this._games
  },
  
  addPlayer(firstName,lastName,age) {
   let player = {
    firstName,
    lastName,
    age }
   this.players.push(player)
  }
}

team.addPlayer('Steph', 'Curry', 28)
team.addPlayer('Lisa', 'Leslie', 44)

console.log(team.players)

The line that I’m talking about is this.players.push(player).

Thanks!

The reason this works is because players is returning you _players, which is a reference to an array, so when you add to the array you updating the array that is referenced, not the actual value of _players.

What you cannot do is:

console.log(team.players)
//(3) [{…}, {…}, {…}]
//0: {firstName: "Pablo", lastName: "Sanchez", age: 11}
//1: {firstName: "Steph", lastName: "Curry", age: 28}
//2: {firstName: "Lisa", lastName: "Leslie", age: 44}
//length: 3

team.players = [{
    firstName: 'Pablo',
    lastName: 'Sanchez',
    age: 11
    },
                {
    firstName: 'Pablo1',
    lastName: 'Sanchez',
    age: 11
    }]

console.log(team.players)
//(3) [{…}, {…}, {…}]
//0: {firstName: "Pablo", lastName: "Sanchez", age: 11}
//1: {firstName: "Steph", lastName: "Curry", age: 28}
//2: {firstName: "Lisa", lastName: "Leslie", age: 44}
//length: 3

If you add a basic setter to players to just update the value to _players you would get this:

console.log(team.players)
//(3) [{…}, {…}, {…}]
//0: {firstName: "Pablo", lastName: "Sanchez", age: 11}
//1: {firstName: "Steph", lastName: "Curry", age: 28}
//2: {firstName: "Lisa", lastName: "Leslie", age: 44}
//length: 3

team.players = [{
    firstName: 'Pablo',
    lastName: 'Sanchez',
    age: 11
    },
                {
    firstName: 'Pablo1',
    lastName: 'Sanchez',
    age: 11
    }]

console.log(team.players)
//(2) [{…}, {…}]
//0: {firstName: "Pablo", lastName: "Sanchez", age: 11}
//1: {firstName: "Pablo1", lastName: "Sanchez", age: 11}
//length: 2