We will explain the functions for finding powers , square roots , and cube roots .
Functions are provided to find powers, square roots, and cube roots.
In this article, we will explain how to use each function and introduce sample programs.
Functions to find powers, square roots, and cube roots
Use the following Math object methods to calculate powers, square roots, and cube roots.
method | explanation |
---|---|
Math.pow(base, exponent) | Returns the power. Returns base raised to the power of exponent. |
Math.sqrt(x) | Returns the square root of the given number. It takes one argument, x, and returns the square root of x. |
Math.cbrt(x) | Returns the cube root of the given number. It takes one argument, x, and returns the cube root of x. |
Below are samples using each method.
console.log(Math.pow(2, 3)); // 8
console.log(Math.sqrt(9)); // 3
console.log(Math.cbrt(8)); // 2
You can also use “**” to calculate powers.
console.log(2 ** 3) // 8
Available from ES2016, it allows you to calculate powers more concisely than Math.pow().
Summary of “How to find powers, square roots, and cube roots”
Below is a summary of how to find powers, square roots, and cube roots.
- Use Math.pow(base, exponent) to find base raised to the power of exponent.
- Find the square root of x using Math.sqrt(x).
- Find the cube root of x using Math.cbrt(x).
Convenient functions are provided for mathematical calculations.
This time we talked about functions for finding powers, square roots, and cube roots, but JavaScript has many other functions.
Please try your hand at mathematical calculations using JavaScript.
Comments