Note: Questions like this change over time as browser engines change how their optimizations work. For a recent look comparing:

Math.pow(x1, 2)
x1 * x1
x1 ** 2                  // ES6 syntax

See this revised performance test and run it in the browsers you care about: https://jsperf.com/math-pow-vs-simple-multiplication/32.

As of April 2020, Chrome, Edge and Firefox show less than 1% difference between all three of the above methods.

If the jsperf link is not working (it seems to be occasionally down), then you can try this perf.link test case.

Original Answer from 2014:

All performance questions should be answered by measurement because specifics of the browser implementation and the particular scenario you care about are often what determine the outcome (thus a theoretical discussion is not always right).

In this case, performance varies greatly by browser implementation. Here are are results from a number of different browsers in this jsperf test: http://jsperf.com/math-pow-vs-simple-multiplication/10 which compares:

Math.pow(x1, 2)
x1 * x1

Longer bars are faster (greater ops/sec). You can see that Firefox optimizes both operations to be pretty much the same. In other browsers, the multiplication is significantly faster. IE is both the slowest and shows the greatest percentage difference between the two methods. Firefox is the fastest and shows the least difference between the two.

Answer from jfriend00 on Stack Overflow
Top answer
1 of 3
105

Note: Questions like this change over time as browser engines change how their optimizations work. For a recent look comparing:

Math.pow(x1, 2)
x1 * x1
x1 ** 2                  // ES6 syntax

See this revised performance test and run it in the browsers you care about: https://jsperf.com/math-pow-vs-simple-multiplication/32.

As of April 2020, Chrome, Edge and Firefox show less than 1% difference between all three of the above methods.

If the jsperf link is not working (it seems to be occasionally down), then you can try this perf.link test case.

Original Answer from 2014:

All performance questions should be answered by measurement because specifics of the browser implementation and the particular scenario you care about are often what determine the outcome (thus a theoretical discussion is not always right).

In this case, performance varies greatly by browser implementation. Here are are results from a number of different browsers in this jsperf test: http://jsperf.com/math-pow-vs-simple-multiplication/10 which compares:

Math.pow(x1, 2)
x1 * x1

Longer bars are faster (greater ops/sec). You can see that Firefox optimizes both operations to be pretty much the same. In other browsers, the multiplication is significantly faster. IE is both the slowest and shows the greatest percentage difference between the two methods. Firefox is the fastest and shows the least difference between the two.

2 of 3
34

In ES6 you can do the following with Exponentiation (x ** y), which produces the same result as Math.pow(x,y):

function squareIt(number) {
  return number ** 2;
}

console.log(squareIt(5));

or you can use a JavaScript library called BigInteger.js for the purpose.

alert(bigInt(5).square());
<script src="https://cdnjs.cloudflare.com/ajax/libs/big-integer/1.6.40/BigInteger.min.js"></script>

🌐
Math.js
mathjs.org › docs › reference › functions › square.html
math.js | an extensive math library for JavaScript and Node.js
math.square(2) // returns number 4 math.square(3) // returns number 9 math.pow(3, 2) // returns number 9 math.multiply(3, 3) // returns number 9 math.map([1, 2, 3, 4], math.square) // returns Array [1, 4, 9, 16]
🌐
W3Schools
w3schools.com › jsref › jsref_sqrt.asp
JavaScript Math sqrt() Method
❮ Previous JavaScript Math Object ... = Math.sqrt(64); let e = Math.sqrt(-9); Try it Yourself » · The Math.sqrt() method returns the square root of a number....
🌐
Medium
rustcodeweb.medium.com › the-simple-trick-to-squaring-numbers-in-javascript-c1ce2eaca1f8
The Simple Trick to Squaring Numbers in JavaScript! | by RustcodeWeb | Medium
August 18, 2024 - Math.pow(number, 2) raises the number to the power of 2, effectively squaring it. The exponentiation operator (**) is a more recent addition to JavaScript, providing a concise syntax for exponentiation.
🌐
p5.js
p5js.org › reference › p5 › square
square
The version of square() with four parameters creates a rounded square.
🌐
Altcademy
altcademy.com › blog › how-to-square-a-number-in-javascript
How to square a number in JavaScript - Altcademy.com
June 9, 2023 - In this example, we first declare a variable number and assign it the value 5. Then, we use the Math.pow() method to calculate the square of the number and store the result in a variable called square.
Find elsewhere
🌐
Square
developer.squareup.com › docs › sdks › nodejs
Node.js SDK
The Square Node.js library supports Square APIs in a language-idiomatic way that reduces complexity without sacrificing API functionality.
🌐
Sabe
sabe.io › blog › javascript-square-number
How to Square a Number in JavaScript - Sabe.io
March 4, 2022 - In this post, we explored the three best ways to square a number in JavaScript, using the Math.pow(), **, and a custom helper square() function.
🌐
Programiz
programiz.com › javascript › examples › square-root
JavaScript Program to Find the Square Root
If 0 or a positive number is passed in the Math.sqrt() method, then the square root of that number is returned. If a negative number is passed, NaN is returned. If a string is passed, NaN is returned. ... Before we wrap up, let’s put your knowledge of JavaScript Program to Find the Square ...
🌐
Codecademy
codecademy.com › forum_questions › 4fc0326bd12f0a0003005677
I don't get how the square function works | Codecademy
The square function is basically telling the computer to multiply (“this number” by “the same number”).
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › pow
Math.pow() - JavaScript - MDN Web Docs
In addition, the behavior where base is 1 and exponent is non-finite (±Infinity or NaN) is different from IEEE 754, which specifies that the result should be 1, whereas JavaScript returns NaN to preserve backward compatibility with its original behavior. Because pow() is a static method of Math, use it as Math.pow(), rather than as a method of a Math object you created (Math is not a constructor). js · // Basic cases Math.pow(7, 2); // 49 Math.pow(7, 3); // 343 Math.pow(2, 10); // 1024 // Fractional exponents Math.pow(4, 0.5); // 2 (square root of 4) Math.pow(8, 1 / 3); // 2 (cube root of 8)
🌐
SheCodes
shecodes.io › athena › 10776-how-to-use-the-square-sum-function-in-javascript
[JavaScript] - How to use the square sum function in | SheCodes
Learn how to create a JavaScript function that returns the sum of squared numbers using the reduce method.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Math › sqrt
JavaScript Math sqrt() - Calculate Square Root | Vultr Docs
September 27, 2024 - The Math.sqrt() function in JavaScript is a standard method used to calculate the square root of a number.
🌐
Team Treehouse
teamtreehouse.com › community › what-is-the-best-way-to-square-a-number-in-javascript
What is the best way to square a number in JavaScript? (Example) | Treehouse Community
May 16, 2016 - Michael Lowe is having issues with: Let's say I want to find 4 squared, i.e. 4^2 or 4 to the power of 2. Right now I have this: var x = 5; var x_squared = M...
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Find a square number - JavaScript - The freeCodeCamp Forum
February 13, 2022 - I am trying out kata on codewars and I got a kata that ask for a square, a perfect square which has same item on row and col. I am not a maths person and I had to google to find what is square number, but google says 3*3 = 9 which is a square number but the kata says it is wrong.