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
在这里,我们检索的是 1 的以 10 为底的对数 -
<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>
输出
正如我们在输出中看到的,它返回了 -Infinity 作为结果。
示例 4
如果我们提供的参数小于 0,则此方法返回 NaN(不是数字)-
<html>
<body>
<script>
const result = Math.log10(-5);
document.write(result);
</script>
</body>
</html>
输出
正如我们在输出中看到的,它返回了 NaN 作为结果。