JavaScript Math.log10() Method



The Math.log10() method in JavaScript accepts a number as an argument (should be greater than 0) and retrieves the base 10 logarithm of that number.

It's important to note that the argument passed to Math.log10() must be a positive number. If a non-positive number is passed, Math.log10() will return NaN (Not a Number). Additionally, if NaN or Infinity is passed as an argument to this method, it will return the same value.

Syntax

Following is the syntax of JavaScript Math.log10() method −

 Math.log10(x) 

Parameters

This method accepts only one parameter. The same is described below −

  • x: A numeric value.

Return value

This method returns the base 10 logarithm (logarithm to the base 10) of the provided number.

Example 1

In the following example, we are calculating the base 10 logarithm of 5 using the Math.log10() method in JavaScript −

 <html> <body> <script> const result = Math.log10(5); document.write(result); </script> </body> </html> 

Output

After executing the above program, it returns approximately 0.6989 as result.

Example 2

Here, we are retrieving the base 10 logarithm of 1 −

 <html> <body> <script> const result = Math.log10(1); document.write(result); </script> </body> </html> 

Output

It returns 0 as result.

Example 3

If we provide 0 or -0 as an argument to this method, it returns -Infinity as result −

 <html> <body> <script> const result1 = Math.log10(0); const result2 = Math.log10(-0); document.write(result1, "<br>", result2); </script> </body> </html> 

Output

As we can see in the output, it returned -Infinity as result.

Example 4

If we provided argument is less than 0, this method returns NaN (not a number) −

 <html> <body> <script> const result = Math.log10(-5); document.write(result); </script> </body> </html> 

Output

As we can see in the output, it returned NaN as result.

Advertisements