Why does Math.log(0) return -Infinity in JavaScript?

When my Math.log value getting zero it is giving -Infinity value. Why it is giving negative Infinity value? Why only giving -Infinity rather giving +Infinity. Why only 0 value is giving this result.

console.log(Math.log(0))

4 thoughts on “Why does Math.log(0) return -Infinity in JavaScript?”

  1. Fundamentally, because that’s how it’s specified:

    20.2.2.20 Math.log ( x )

    Returns an implementation-dependent approximation to the natural logarithm of x.

    • If x is NaN, the result is NaN.
    • If x is less than 0, the result is NaN.
    • If x is +0 or -0, the result is -∞.
    • If x is 1, the result is +0.
    • If x is +∞, the result is +∞.

    (my emphasis)

    But more to the point, log(x) as x approaches 0 decreases without bound. So -Infinity seems a reasonable choice for the spec to make.

    Reply

Leave a Comment