The JavaScript log function is a mathematical function useful for calculating the logarithmic value of a given number with base e.
JavaScript log syntax
The syntax of the math log Function is
Math.log(number);
Parameter: The Number argument can be any numerical expression, and it represents the exponent value.
Return Value
- This function returns the output if the number argument is a positive value.
- When it is a Negative or not a number, the math.log returns NaN (Not a Number).
- If it is Null, the logarithmic function changes the Null value to zero.
- If it is zero, it will return a Negative Infinity.
JavaScript log Function Example
In this example, we will find the JavaScript logarithmic value of different data types and display the output.
<!DOCTYPE html>
<html>
<head>
<title> JavaScriptLOGFunction </title>
</head>
<body>
<h1> JavaScriptLOGFunction </h1>
<p id = "Pos"></p>
<p id = "Zero"></p>
<p id = "Dec"></p>
<p id = "Neg_Dec"></p>
<p id = "Str"></p>
<p id = "Exp"></p>
<p id = "Null"></p>
<p id = "Multi"></p>
<script>
document.getElementById("Pos").innerHTML = Math.log(1);
document.getElementById("Zero").innerHTML = Math.log(0);
document.getElementById("Dec").innerHTML = Math.log(10.45);
document.getElementById("Neg_Dec").innerHTML = Math.log(-6.45);
document.getElementById("Str").innerHTML = Math.log("JavaScript");
document.getElementById("Null").innerHTML = Math.log(null);
document.getElementById("Multi").innerHTML = Math.log(2 + 7 - 5);
</script>
</body>
</html>

TIP: Please refer to the log2() and log10() methods from the Math functions page to find the base 2 and base 10 logarithmic values.
Example 2: When we pass positive or negative zero, the Math.log() function returns a negative infinity value.
console.log(Math.log(0));
console.log(Math.log(-0));
-Infinity
-Infinity
Example 3: When we pass a positive infinity, the Math.log() function returns a positive infinity. However, if the parameter value is a negative infinity or NaN, it returns NaN (not a number) as the output.
console.log(Math.log(Infinity));
console.log(Math.log(-Infinity));
console.log(Math.log(NaN));
Infinity
NaN
NaN
JavaScript Math.log() on Negative Numbers
The Math.log() function does not find the logarithmic value of negative numbers; instead, it returns NaN as the output.
console.log(Math.log(-1));
console.log(Math.log(-10));
console.log(Math.log(-2.5));
NaN
NaN
NaN
What is the difference between Math.log() and Math.log10()?
The log() function returns the natural logarithmic value of a given number base e. On the other hand, log10() returns the base 10 logarithmic value of a number.
Related Math Posts