Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
boolean does not change in function JavaScript

I have the following code:

const ships = (length) => {
  isSunk = false
  console.log('BEFORE: ' + isSunk)
  const shipSize = length
  let hits = []
  const hitLocation = location => {
    hits.push(location);
    isSunk = true;
    console.log('INSIDE: ' + isSunk)
  }
  console.log('AFTER: ' + isSunk)
  return {
    isSunk,
    hitLocation,
    hits,
    shipSize
  }
}

const two = ships(2)
two.hitLocation(1)
two.hitLocation(2)
console.log('FINAL: ' + two.isSunk)
console.log('HITS: ' + two.hits)

Is there a reason why isSunk is not saved as true when I call it in the end?

Is it due to the nested functions?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

When you set the boolean into the object, it is that value. It is not a reference to the variable. So when you change the variable, the value you saved inside of the object that you returned is not updated.

You can use a function to get the value

const ships = (length) => {
  isSunk = false
  console.log('BEFORE: ' + isSunk)
  const shipSize = length
  let hits = []
  const hitLocation = location => {
    hits.push(location);
    isSunk = true;
    console.log('INSIDE: ' + isSunk)
  }
  console.log('AFTER: ' + isSunk)
  return {
    isSunk: () => isSunk,
    hitLocation,
    hits,
    shipSize
  }
}

const two = ships(2)
two.hitLocation(1)
two.hitLocation(2)
console.log('FINAL: ' + two.isSunk())

Other option is to use a class.

class Ship {

    hits = [];
    
    constructor(length) {
        this.shipSize = length;
    }
    
    get isSunk() {
        return this.hits.length === this.shipSize;
    }
    
    hitLocation (location) {
      this.hits.push(location);
      console.log('INSIDE: ' + this.isSunk)
    }
    
}


const two = new Ship(2)
two.hitLocation(1)
two.hitLocation(2)
console.log('FINAL: ' + two.isSunk)

about 4 years ago · Juan Pablo Isaza Relatório

0

The reason this happens is that isSunk is just a local variable and a value in contrast to hits which is also a local variable but an array and thus just a reference not a value. As you return the object when calling ship() these values and the reference get returned in an object.

Now when you call hitLocation() it uses the local variable hits to add an entry to the array and as the array is just a reference the local variable hits and the hits in the two object have the same reference so they point to the same memory location, thus the update can be seen using the returned object.

On the other hand hitLocation() also modifies the local variable isSunk but as you do not return that and it is not a reference the object stored in variable two is not updated.

The best way to fix this to my mind is using a class instead of a object here. This will be much more concise and clear than returning and/or passing the object you want to perform some action on to a function all the time.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda