Languages
[Edit]
EN

JavaScript - Math.pow() method example

13 points
Created by:
Marcin
2465

Math.pow() is static method that returns base raised to the power of exponent (value^exponent operation).

// ONLINE-RUNNER:browser;

//                     base  exponent
console.log( Math.pow(  1  ,  2       ) ); // 1
console.log( Math.pow(  2  ,  2       ) ); // 4
console.log( Math.pow(  3  ,  2       ) ); // 9

console.log( Math.pow(  0  ,  3       ) ); // 0
console.log( Math.pow(  0.5,  0.3     ) ); // 0.8122523963562356
console.log( Math.pow( -1  ,  4       ) ); // 1

console.log( Math.pow(  0  , -0.4     ) ); // Infinity
console.log( Math.pow( -0.5, -2       ) ); // 4
console.log( Math.pow( -2.0, -2       ) ); // 0.25
console.log( Math.pow(  2.0 , 0.5     ) ); // NaN

1. Documentation

SyntaxMath.pow(base, exponent)
Parameters

The method takes integer or float number arguments (primitive values).

  • base - base of power
  • exponent - exponent used with base
Result

number value (primitive value) calculated as base raised to the power of expponent or NaN if operation can not be executed.

Descriptionpow is static method that returns base raised to the power of expponent (value^exponent operation).

2. Exponentiation operator (**) example

// ONLINE-RUNNER:browser;

console.log(   1    **  2   ); // 1
console.log(   2    **  2   ); // 4
console.log(   3    **  2   ); // 9

console.log(   0    **  3   ); // 0
console.log(   0.5  **  0.3 ); // 0.8122523963562356
console.log( (-1)   **  4   ); // 1

console.log(   0    ** -0.4 ); // Infinity
console.log( (-0.5) ** -2   ); // 4
console.log( (-2.0) ** -2   ); // 0.25
console.log( (-2.0) **  0.5 ); // NaN

Note: this feature has been introduced in ES2016.

JavaScript - Math (EN)

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.