JavaScript log10

The JavaScript log10 function is one of the math functions that returns the base 10 logarithmic value of a given positive number greater than zero. The syntax of the log10 function is

Math.log10(number)

Parameter: It has to be a positive number greater than 0.

Return Value: It returns the base 10 logarithm of the given number. If the parameter value is less than 0, it returns NaN.

JavaScript log10 example

In the following example, we use the log10 function on various numeric (integer) values to find the base 10 logarithm. For more, please refer to the math functions article on the JavaScript page.

console.log(Math.log10(2000)); 
console.log(Math.log10(590));
console.log(Math.log10(100));
console.log(Math.log10(10));
3.3010299956639813 
2.7708520116421442 
2 
1

Example 2: If we use the Math.log10() function on positive or negative zero, it returns -Infinity as the output.

console.log(Math.log10(0)); 
console.log(Math.log10(-0));
-Infinity 
-Infinity

Example 3: If we pass Infinity to the Math.log10() function, it returns the same. However, when we pass -Infinity or NaN, it returns NaN as output.

console.log(Math.log10(Infinity)); 
console.log(Math.log10(-Infinity));
console.log(Math.log10(NaN));
Infinity 
NaN 
NaN

Using JavaScript Math.log10() on negative numbers

The log10() function finds the base 10 logarithm of a positive number greater than 0. If we pass a negative number, it returns NaN (Not a Number) as output.

console.log(Math.log10(-200)); 
console.log(Math.log10(-100));
NaN 
NaN

Using Math.log10() on string numeric values

If we use the Math.log10() function on string data. If the given string value is a numeric one, it is converted to a number, and the log10 function is applied. When we pass a string as text, it cannot be converted to a number, so the result is NaN (Not a Number).

For the natural logarithm, please refer to log(), and for the base 2 logarithm, you can just refer to the log2 method.

console.log(Math.log10("850")); 
console.log(Math.log10("1250"));
console.log(Math.log10("hi"));
2.929418925714293 
3.0969100130080562 
NaN

Related Math Posts