x = -0
>> -0
typeof(x)
>> "number"
x.toString()
>> "0"
console.log(x)
>> -0
How can I convert Javascript's −0 (number zero with the sign bit set rather than clear) to a two character string ("-0") in the same way that console.log does before displaying it?
If Node.js (or npm available¹)
util.inspectwill do that:If not, you can make a function:
Substitute the condition with
x === 0 && 1 / x === -Infinityif you don’t haveObject.is.¹ I haven’t read this package’s source and it could be updated in the future anyway; look at it before installing!