Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

236
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!