2

As reported in the title:
Why in Javascript -1 * 0 = -0 ?

enter image description here

How I can prevent it?
I would have expected to have 0 as result.

gixlg
  • 1,193
  • 1
  • 9
  • 21

1 Answers1

1

Do you have a specific reason for wanting to prevent that?

If your concern is related to the fact that you want to get 0 instead of -0 you don't need to worry about that since -0 === 0 return true

console.log(-1 * 0)
console.log(-1 * 0 === 0)

const turnItToNegative = n => -1 * n 

const myNumber = turnItToNegative(0)

if (myNumber === 0) {
  console.log('This is still zero, don\'t worry')
}
Mestre San
  • 1,806
  • 15
  • 24
  • @gixlg That isn't a problem: `\`${-0}\`==="0"` – user120242 May 28 '20 at 21:21
  • @gixlg Are you actually getting an unexpected result (outside of the console)? If so, show the code that's producing the unexpected result. If not, then you're worrying about nothing. – John Montgomery May 28 '20 at 21:25
  • Ya, the only time you'd have an issue is if you need to differentiate between -0 and 0. But you'd know it if you needed it. – user120242 May 28 '20 at 21:30