HOWTO · JavaScript

How to Square a Number in JavaScript

This tutorial explains how to square a number in JavaScript.

On this page

To square a value in JavaScript, raise it to the second power with number ** 2. For example, 5 ** 2 evaluates to 25. This is the clearest choice for ordinary Number values; multiplication and Math.pow() are also available.

Square a Number in JavaScript

A square is a value multiplied by itself. JavaScript’s Number type is the usual choice for everyday calculations, including integers and decimals. The following standalone example shows the direct expression and logs its result:

const number = 5;
console.log(number ** 2);

The value after ** is the exponent. Use 2 to square, 3 to cube, or another exponent for a different power. This operation works with negative and fractional Number values as well; for example, squaring a negative value produces a nonnegative result. JavaScript numbers use floating-point arithmetic, so very large or precise decimal calculations may not retain every mathematical digit.

Use the Exponentiation Operator

The ** token is the exponentiation operator, not a method. Its left operand is the base and its right operand is the exponent, so base ** 2 expresses squaring directly. For a variable, assign the expression just like any other calculation:

const base = 8;
const squared = base ** 2;
console.log(squared);

Parenthesize a negative base when writing it as a literal: (-3) ** 2 squares negative three. Without parentheses, unary negation and exponentiation do not have the same grouping, and -3 ** 2 is a syntax error. If the value is already stored in a variable, value ** 2 needs no extra parentheses.

Use Multiplication or Math.pow()

You can also multiply a value by itself. This can be easy to read when describing the operation explicitly, while ** makes the exponent visible. The older built-in Math.pow(base, exponent) function expresses the same base-and-exponent relationship and remains useful in code that uses that style.

const number = 5;
const byMultiplication = number * number;
const byMathPow = Math.pow(number, 2);
console.log(byMultiplication, byMathPow);

For ordinary Number inputs, all three forms calculate a power using JavaScript’s floating-point number representation. Choose the notation that best fits the surrounding code; these alternatives do not provide exact arbitrary-precision decimal arithmetic.

Wrap Squaring in a Reusable Function

If the same operation is needed in several places, put it in a function. A named function is convenient when you want a reusable declaration; an arrow function is a compact alternative, including for callbacks. The examples below each define and call one form:

function squareNumber(value) {
  return value ** 2;
}

console.log(squareNumber(6));
const square = (value) => value ** 2;
console.log(square(6));

Both functions accept one value and return its square. They do not validate or convert their input, so callers should pass a value with the numeric behavior they intend. For instance, multiplying a string after implicit conversion can produce surprising results; convert and validate external input before calling the helper when needed.

Square BigInt Values When Exact Integer Precision Matters

JavaScript Number values represent integers exactly only through Number.MAX_SAFE_INTEGER (9,007,199,254,740,991). If integer arithmetic must remain exact beyond that range, use BigInt from the start. Add an n suffix to an integer literal, then use the same exponentiation operator:

const largeInteger = 9007199254740993n;
const squared = largeInteger ** 2n;
console.log(squared);

A BigInt exponent must also be a BigInt, which is why the example uses 2n. BigInt supports integer values, not fractional values. JavaScript does not allow mixing BigInt and Number operands in arithmetic: convert deliberately when appropriate, and remember that converting an already imprecise Number cannot recover digits that were lost. Use Number for ordinary numeric work and BigInt when exact large-integer arithmetic is required.