The Math.pow(x,y) JavaScript method is a member of the Math object class and allows you to compute x to the power of y (x^y). Since the arguments, x and y, can be any value (including strings), the Math.pow(x,y) function will only calculate results that are not imaginary and not complex numbers. If either imaginary or complex numbers are involved in the calculation, the function will return NaN, which stands for "Not a number". In the event that the result exceeds the storage capacity for a floating-point value, Infinity will be returned.
x The number to be raised to a power y The power to raise x to
x^y Return the result of x raised to the power of y. NaN Not a number. The result is either imaginary or complex. Infinity The result is too large to be stored as a floating-point value.
The Math.pow(x,y) function should only be used when computing mathematical operations or formulas. String literals (i.e., "four") should not be used since the Math.pow(x,y) function will return NaN (Not a Number). Only numbers (i.e., integer , floating-point , hexadecimal, or octal literals) may be used as parameters for this function. However, when raising a negative number x to the power of y, y should be a positive or negative integer literal. For example, performing the following operation will result in NaN: Math.pow(-2,4.32) = NaN.
The following example illustrates the basic uses and misuses of the Math.pow(x,y) function.
JavaScript Code Output <script language="JavaScript"> <!-- document.writeln ("5^2 = " + Math.pow(5,2)); // --> </script><script language="JavaScript"> <!-- document.writeln ("-3^4 = " + Math.pow(-3,4)); // --> </script><script language="JavaScript"> <!-- document.writeln ("-2.5^-5 = " + Math.pow(-2.5,-5)); // --> </script><script language="JavaScript"> <!-- document.writeln ("999^100 = " + Math.pow(999,100)); // --> </script><script language="JavaScript"> <!-- document.writeln ("0x5 ^ 0x0a = " + Math.pow(0x5,0x0a)); // hex literal // --> </script><script language="JavaScript"> <!-- document.writeln ("0377 ^ 02 = " + Math.pow(0377,02)); // octal literal // --> </script><script language="JavaScript"> <!-- document.writeln ("-2^4.32 = " + Math.pow(-2,4.32)); // --> </script><script language="JavaScript"> <!-- document.writeln ("two^four = " + Math.pow("two","four")); // --> </script><script language="JavaScript"> <!-- document.writeln ("999^999 = " + Math.pow(999,999)); // --> </script>Example 1.0
The following example shows how you can create a function, called powersOf2(y), which raises 2 to the value of y passed in as a parameter to the function. Immediately following the function declaration is a for loop which displays the first 10 powers of 2 starting from 0. Although its good programming practice to declare your functions in the <head> tag of the HTML document, I just created a function in the <body> of the document to illustrate this example.
JavaScript Code Output <script language="JavaScript"> <!-- function powersOf2 (y) { return Math.pow(2,y); } for (var i = 0; i < 10; i++) { document.writeln ("2 ^ " + i + " = " + powersOf2(i)); document.writeln ("<br>"); } // --> </script>Example 2.0
The Math.pow(x,y) function can also be nested as shown in the following example.
JavaScript Code Output <script language="JavaScript"> <!-- document.writeln ("2 ^ (2 ^ 2) = " + Math.pow(2,Math.pow(2,2))); // --> </script>Example 3.0
The Math.pow(x,y) function is compatible with:
- Netscape 4.x and higher
- Internet Explorer 4.x and higher
I have successfully tested the Math.pow(x,y) function on two browsers (Netscape 6.2 and IE 6) as shown below.
Table 1.0: Tested Browser Compatibility for Math.pow(x,y) function Internet Explorer 6 Netscape Navigator 6.2 Math.pow(x,y) Compatible Compatible
ICS 415 | Topic Notes | Assignments | Feedback | Links
Last Updated: March 14, 2002