JavaScript 的 pow 函数是一个 Math 函数,用于计算指定表达式的幂,其语法是:
Math.pow(base, Exponent);
- Base:请在此处指定基数。
- Exponent:请在此处指定指数或幂。
例如,如果 x 是基数,2 是指数,则 Math.pow(x, 2) = x²。
- 如果 Base 或 Exponent 的值为非数字,pow 函数将返回 NaN。
- 如果 Base 或 Exponent 的值为 Null,它将返回 Zero。
JavaScript pow 函数示例
pow 函数返回给定数字的幂。在此示例中,我们将计算不同数据类型的幂并显示输出。
首先,我们在正负小数上使用了 JavaScript pow 函数。从下面的屏幕截图中,您可以看到 pow 函数为负数返回 NaN(非数字)作为输出。
接下来,我们对 Str 使用了 Power 函数的字符串文本。下面的 JavaScript 语句将“3”转换为整数。
对于 Str1,我们对“String”使用了这个 数学方法;正如我们之前所说,它返回 NaN 作为输出。最后,我们将 Null 值用作第二个参数。这里,null 被转换为 Zero。
<!DOCTYPE html>
<html>
<head>
<title> JavaScriptPOWER Function </title>
</head>
<body>
<h1> JavaScriptPOW Function </h1>
<p id = "Pos"></p>
<p id = "Neg"></p>
<p id = "Multi"></p>
<p id = "Dec"></p>
<p id = "Neg_Dec"></p>
<p id = "Str"></p>
<p id = "Str1"></p>
<p id = "Null"></p>
<script>
document.getElementById("Pos").innerHTML = Math.pow(2, 3);
document.getElementById("Neg").innerHTML = Math.pow(-2, 3);
document.getElementById("Multi").innerHTML = Math.pow(2, -3);
document.getElementById("Dec").innerHTML = Math.pow(2.50, 4.05);
document.getElementById("Neg_Dec").innerHTML = Math.pow(-3.10,-6.05);
document.getElementById("Str").innerHTML = Math.pow(2, "3");
document.getElementById("Str1").innerHTML = Math.pow(2, "String");
document.getElementById("Null").innerHTML = Math.pow(4, null);
</script>
</body>
</html>
