How to find the square of x in JavaScript: 1. Use the power operator "**" to return the first operand as the base and the second operand as the power of the exponent. The syntax "x **" 2"; 2. Use the pow() method to return the y power of x, with the syntax "Math.pow(x,2)".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JavaScript to find the square of x
1. Use the power operator
Power operator **
Returns the first operand as the base, and the second operand as the power of the exponent.
Example:
The following code means finding the 2nd power of 6 (i.e. square), which is equivalent to 6 * 6. Based on the mathematics you have learned, you can get the result to be 36:
var x = 6; var y = x ** 2; console.log(y); // 36
2. Use pow() method
pow() method returns x raised to the y power.
Example:
var x = 6; var y = Math.pow(x,2); console.log(y); // 36
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to find x squared in JavaScript. For more information, please follow other related articles on the PHP Chinese website!