JavaScript Math.log10()

Math.log10() 方法返回数字以 10 为底的对数。在数学上,它等同于 log10(x)

示例

// calculate the base 10 log of 100 var value = Math.log10(100);
console.log(value); // Output: 2

log10() 语法

log10() 的语法是

Math.log10(x)

在这里,log10() 是一个静态方法。因此,我们需要通过类名 Math 来访问该方法。


log10() 参数

log10() 方法接收

  • x - 一个数字

log10() 返回值

log10() 方法返回

  • 给定数字以 10 为底的对数。
  • 对于负数和非数字参数,返回 NaN

示例 1: JavaScript Math.log10()

// find the base 10 log value of 1 var value1 = Math.log10(1);
console.log(value1);
// find the base 10 log value of 10 var value2=Math.log10(10);
console.log(value2)

输出

0
1

在上面的例子中:

  • Math.log10(1) - 计算 1 的以 10 为底的对数
  • Math.log10(10) - 计算 10 的以 10 为底的对数

示例 2: log10() 与 0

// find the base 10 log value of 0 var value = Math.log10(0);
console.log(value); // Output: -Infinity

在上面的示例中,我们使用 log10() 方法计算 0 的以 10 为底的对数。

输出 -Infinity 表示 0 的以 10 为底的对数是负无穷大。


示例 3: log10() 与负值

// find the base 10 log of negative values var value = Math.log10(-1);
console.log(value); // Output: NaN

在上面的示例中,我们使用 log10() 方法计算负数的以 10 为底的对数。

输出 NaN 代表 不是数字。我们得到这个结果是因为负数的以 10 为底的对数是未定义的。


另请阅读

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战