Math.pow()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
Math.pow() は静的メソッドで、基数をべき乗した値を返します。
試してみましょう
console.log(Math.pow(7, 3));
// 予想される結果: 343
console.log(Math.pow(4, 0.5));
// 予想される結果: 2
console.log(Math.pow(7, -2));
// 予想される結果: 0.02040816326530612
// (1/49)
console.log(Math.pow(-7, 0.5));
// 予想される結果: NaN
構文
js
Math.pow(base, exponent)
引数
返値
base を表す数値を exponent 乗した値。以下のいずれかの場合は、 NaN を返します。
exponentがNaNである。baseがNaNで、exponentが0以外である。baseが ±1 で、exponentが ±Infinityである。base < 0で、exponentが整数ではない。
解説
Math.pow() は ** 演算子と同等ですが、 Math.pow() は数値のみを受け入れるという点が異なります。
Math.pow(NaN, 0)(および同等の NaN ** 0)は、 NaN が数学演算で伝播しない唯一のケースです。これは、オペランドが NaN であるにもかかわらず 1 を返します。さらに、 base が 1 で exponent が無限大(±Infinity または NaN)である場合の動作は、結果が 1 となることを規定している IEEE 754 とは異なり、 JavaScript では元の動作との後方互換性を維持するために NaN を返します。
pow() は Math の静的メソッドなので、常に Math.pow() として使用し、自分で Math オブジェクトを生成してそのメソッドとして使用しないでください。 (Math にはコンストラクターがありません)。
例
>Math.pow() の使用
js
// 基本的な例
Math.pow(7, 2); // 49
Math.pow(7, 3); // 343
Math.pow(2, 10); // 1024
// 小数のべき乗
Math.pow(4, 0.5); // 2 (4 の平方根)
Math.pow(8, 1 / 3); // 2 (8 の立方根)
Math.pow(2, 0.5); // 1.4142135623730951 (2 の平方根)
Math.pow(2, 1 / 3); // 1.2599210498948732 (2 の立方根)
// 負の数のべき乗
Math.pow(7, -2); // 0.02040816326530612 (1/49)
Math.pow(8, -1 / 3); // 0.5
// 負の数の底
Math.pow(-7, 2); // 49 (2 乗は正の数)
Math.pow(-7, 3); // -343 (3 乗は負の数)
Math.pow(-7, 0.5); // NaN (負の数には実数の平方根がない)
// Due to "even" and "odd" roots laying close to each other,
// and limits in the floating number precision,
// negative bases with fractional exponents always return NaN,
// even when the mathematical result is real
Math.pow(-7, 1 / 3); // NaN
// Zero and infinity
Math.pow(0, 0); // 1 (任意の数 ** ±0 is 1)
Math.pow(Infinity, 0.1); // Infinity (正の指数)
Math.pow(Infinity, -1); // 0 (負の指数)
Math.pow(-Infinity, 1); // -Infinity (正の奇数の整数の指数)
Math.pow(-Infinity, 1.5); // Infinity (正の指数)
Math.pow(-Infinity, -1); // -0 (負の奇数の整数の指数)
Math.pow(-Infinity, -1.5); // 0 (負の指数)
Math.pow(0, 1); // 0 (正の指数)
Math.pow(0, -1); // Infinity (負の指数)
Math.pow(-0, 1); // -0 (正の奇数の整数の指数)
Math.pow(-0, 1.5); // 0 (正の指数)
Math.pow(-0, -1); // -Infinity (負の奇数の整数の指数)
Math.pow(-0, -1.5); // Infinity (負の指数)
Math.pow(0.9, Infinity); // 0
Math.pow(1, Infinity); // NaN
Math.pow(1.1, Infinity); // Infinity
Math.pow(0.9, -Infinity); // Infinity
Math.pow(1, -Infinity); // NaN
Math.pow(1.1, -Infinity); // 0
// NaN: only Math.pow(NaN, 0) does not result in NaN
Math.pow(NaN, 0); // 1
Math.pow(NaN, 1); // NaN
Math.pow(1, NaN); // NaN
仕様書
| Specification |
|---|
| ECMAScript® 2026 Language Specification> # sec-math.pow> |
ブラウザーの互換性
Loading…