Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find array, unexpected 0 !== 0

Can’t get why this works this way?
To which type js converts item with value = 0 and why ?

if([1,2,0,1].find(item => item === 0)) {
    console.log(1); // logs nothing
}

console.log(0 === 0); // logs true

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

find returns the element it found, and 0 is falsy so if(0) never runs the conditional block.

You should use if (arr.includes(0)). Alternatives that would also work, but are more ugly, are

  • if (arr.some(item => item === 0))
  • if (arr.findIndex(item => item === 0) > -1)
  • if (arr.find(item => item === 0) !== undefined)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading