JavaScript Math.log10() 方法
JavaScript 中的 Math.log10() 方法接受一个数字作为参数(应大于 0),并获取该数字以 10 为底的对数。
需要注意的是,传递给 Math.log10() 的参数必须是正数。如果传递的是非正数,Math.log10() 将返回 NaN(非数字)。此外,如果将 NaN 或 Infinity 作为参数传递给此方法,它将返回相同的值。
语法
以下是 JavaScript Math.log10() 方法的语法 -
Math.log10(x)
参数
此方法仅接受一个参数。下文同样描述 -
- x: 一个数值。
返回值
此方法返回所提供数字的以 10 为底的对数(以 10 为底的对数)。
示例 1
在下面的示例中,我们使用 JavaScript 中的 Math.log10() 方法计算 5 的以 10 为底的对数 -
<html> <body> <script> const result = Math.log10(5); document.write(result); </script> </body> </html>
输出
执行上述程序后,返回结果约为 0.6989。
示例 2
这里,我们获取以 10 为底的 1 的对数 -
<html> <body> <script> const result = Math.log10(1); document.write(result); </script> </body> </html>
输出
返回结果 0。
示例 3
如果我们向此方法提供 0 或 -0 作为参数,则返回结果为 -Infinity -
<html> <body> <script> const result1 = Math.log10(0); const result2 = Math.log10(-0); document.write(result1, "<br>", result2); </script> </body> </html>
输出
正如我们在输出中看到的,它返回了负无穷大。
示例 4
如果我们提供的参数小于 0,则此方法返回 NaN(非数字)-
<html> <body> <script> const result = Math.log10(-5); document.write(result); </script> </body> </html>
输出
正如我们在输出中看到的,它返回了 NaN 作为结果。

