Math.pow()
Базовая поддержка Широко доступна
Эта функция хорошо зарекомендовала себя и работает на многих устройствах и версиях браузеров. Она доступна во всех браузерах с июля 2015 года.
Статический метод Math.pow() возвращает значение основания, возведённого в степень. То есть
Попробуйте
console.log(Math.pow(7, 3)); // Expected output: 343 console.log(Math.pow(4, 0.5)); // Expected output: 2 console.log(Math.pow(7, -2)); // Expected output: 0.02040816326530612 // (1/49) console.log(Math.pow(-7, 0.5)); // Expected output: NaN
Синтаксис
Math.pow(base, exponent)
Параметры
-
base - Число, являющееся основанием.
-
exponent - Число, являющееся показателем степени.
Возвращаемое значение
Число, представляющее base, возведённое в степень exponent. Возвращает NaN в одном из следующих случаев:
-
exponentisNaN. -
baseisNaNandexponentis not0. -
baseis ±1 andexponentis ±Infinity. -
base < 0andexponentis not an integer.
Описание
Math.pow() эквивалентен оператору **, за исключением того, что Math.pow() принимает только числа.
Math.pow(NaN, 0) (и эквивалентный NaN ** 0) — это единственный случай, когда NaN не распространяется через математические операции — он возвращает 1, несмотря на то, что операнд является NaN. Кроме того, поведение, когда base равно 1, а exponent не является конечным (±Infinity или NaN), отличается от IEEE 754, который определяет, что результат должен быть 1, тогда как JavaScript возвращает NaN для сохранения обратной совместимости с его первоначальным поведением.
Поскольку pow() является статическим методом Math, используйте его как Math.pow(), а не как метод объекта Math, который вы создали (Math не является конструктором).
Примеры
Использование Math.pow()
// Basic cases Math.pow(7, 2); // 49 Math.pow(7, 3); // 343 Math.pow(2, 10); // 1024 // Fractional exponents Math.pow(4, 0.5); // 2 (square root of 4) Math.pow(8, 1 / 3); // 2 (cube root of 8) Math.pow(2, 0.5); // 1.4142135623730951 (square root of 2) Math.pow(2, 1 / 3); // 1.2599210498948732 (cube root of 2) // Signed exponents Math.pow(7, -2); // 0.02040816326530612 (1/49) Math.pow(8, -1 / 3); // 0.5 // Signed bases Math.pow(-7, 2); // 49 (squares are positive) Math.pow(-7, 3); // -343 (cubes can be negative) Math.pow(-7, 0.5); // NaN (negative numbers don't have a real square root) // 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 (anything ** ±0 is 1) Math.pow(Infinity, 0.1); // Infinity (positive exponent) Math.pow(Infinity, -1); // 0 (negative exponent) Math.pow(-Infinity, 1); // -Infinity (positive odd integer exponent) Math.pow(-Infinity, 1.5); // Infinity (positive exponent) Math.pow(-Infinity, -1); // -0 (negative odd integer exponent) Math.pow(-Infinity, -1.5); // 0 (negative exponent) Math.pow(0, 1); // 0 (positive exponent) Math.pow(0, -1); // Infinity (negative exponent) Math.pow(-0, 1); // -0 (positive odd integer exponent) Math.pow(-0, 1.5); // 0 (positive exponent) Math.pow(-0, -1); // -Infinity (negative odd integer exponent) Math.pow(-0, -1.5); // Infinity (negative exponent) 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
Спецификации
Совместимость с браузерами
| Десктопные | Мобильные | Серверные | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on iOS | Samsung Internet | WebView Android | WebView on iOS | Bun | Deno | Node.js | |
pow |
1 |
12 |
1 |
3 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
4.4 |
1 |
1.0.0 |
1.0 |
0.10.0 |
Смотрите также
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow