Math.pow and Math.sqrt Examples
Math.pow
The general syntax for this method is:
Math.pow(x, y)
This method calculates and returns the value of x raised to the power of y
(x :sup:y
), and it is identical to the x**y
operation. The pow name
refers to the operation to the power of.
Example
|
|
Console Output
81
81
Math.sqrt
The general syntax for this method is:
Math.sqrt(number)
This method calculates and returns the square root of number
, and it is a
shortcut for using the fraction 1/2 in the pow
method.
Numerical strings can also be evaluated, but should be avoided as a best practice.
Example
|
|
Console Output
9
9
10.535653752852738
6
Math.sqrt
also works on arrays, but must be combined with the
map array method. The syntax for this is:
arrayName.map(Math.sqrt)
Example
|
|
Console Output
[ 1.4142135623730951, 4, 10, 11 ]