AppDividend
Latest Code Tutorials

# JavaScript Math pow() Function Example

JavaScript Math pow() is an inbuilt method that is used to find the value of a number raised to an exponent. Javascript pow() method can come handy in several programs dealing with mathematical expressions.

## JavaScript Math pow()

If you want to find the power of a number in Javascript, then Math.pow() function is used. Since the Math.pow() is a static method of Math and therefore it is always used as Math.pow() and not as a method of an object created of Math class.

### Syntax

`Math.pow(x, y)`

Numbers x and y, such that

x: The base of the power.

y: The exponent to which the base is raised.

### Return Value

The value xy, i.e., x multiplied to itself y times.

See the following figure.

### Note

1. If the base is negative and the exponent is integral, the value returned is positive for even exponent values and negative for odd exponent values.
2. If the base is negative and the exponent is fractional, the method will always return NaN.
3. If the exponent is positive or negative zero, the method will always return 1.

### Compatibility(Version and above)

2. Internet Explorer v3
3. Edge v12
4. Firefox v1
5. Opera v3
6. Safari v1
7. Android webview v1
8. Chrome for Android v18
9. Firefox for Android v4
10. Opera for Android v10.1
11. Safari on iOS v1
12. Samsung Internet v1.0
13. Node.js

JavaScript version: ECMAScript 1

Consider the following examples:

The following example demonstrates the use of this method with both base and exponent as non-negative integral values.

```// app.js

console.log(Math.pow(2, 2));
console.log(Math.pow(2, 3));
console.log(Math.pow(2, 4));
console.log(Math.pow(0, 2));
console.log(Math.pow(6, 3));
```

#### Output

```node app
4
8
16
0
216
```

The following example demonstrates the use of this method with the exponent having a fractional value.

```// app.js

console.log(Math.pow(4, 0.5));		  //square root of 4
console.log(Math.pow(27, 1 / 3)); 		 //cube root of 27
console.log(Math.pow(196, 0.5));		//square root of 196
console.log(Math.pow(16, 1 / 4)); 	           //fourth root of 16
```

#### Output

```node app
2
3
14
2
```

### Example 3

The following example demonstrates the use of this method with the exponent having a negative value.

```// app.js

console.log(Math.pow(1/4,-2));
console.log(Math.pow(4,-0.5));
```

#### Output

```node app
16
0.5
```

### Example 4

The following example demonstrates the use of this method with the base having a negative value and the exponent having an integral value. In such a case, the result will have a negative value for odd exponents and a positive value for even exponents.

See the following code.

```// app.js

console.log(Math.pow(-2,2));
console.log(Math.pow(-2,3));
```

#### Output

```node app
4
-8
```

### Example 5

The following example demonstrates the use of this method with the base having a negative value and the exponent having a fractional value. In such a case, this method always returns NaN.

```// app.js

console.log(Math.pow(-4,0.5));
console.log(Math.pow(-27,1/3));
console.log(Math.pow(-4,-0.5));
```

#### Output

```node app
NaN
NaN
NaN
```

### Example 6

The following example demonstrates the use of this method when the exponent is positive or negative zero. In such a case, this method always returns 1.

```// app.js

console.log(Math.pow(2, 0));
console.log(Math.pow(2, -0));
```

#### Output

```node example6
1
1
```

### Example 7

The following example demonstrates the application of this method in a simple programming context.

Given the side of a square, find its area.

```// app.js

// Given the side of a square, find its area.

let side;

const rl = r.createInterface({
input: process.stdin,
output: process.stdout
});

const prompt1 = () => {
return new Promise((resolve, reject) => {
resolve();
});
});
};

const main = async () => {
await prompt1();
rl.close();

var area = Math.pow(side, 2);
console.log("Area: " + area);
}

main();```

#### Output

```Test Case 1:
->node example7
Side: 2
Area: 4

Test Case2:
->node example7
Side: 4
Area: 16

```