Check Cube Number in JavaScript

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code examples + Try it
Number theory

What you’ll learn

  • What it means for an integer to be a perfect cube (n = k3).
  • A compact check using Math.cbrt and Math.round (with a final BigInt cube test), plus an all-integer loop like the range sample.
  • Sign, zero, overflow, and floating-point caveats, plus a browser live preview.

Overview

Given an integer n, decide whether n = k3 for some integer k. One approach uses the real cube root from Math.cbrt; we round it correctly for negatives, then verify with exact integer cubing (BigInt).

Two programs

Math.cbrt + Math.round for a single value, then a linear k scan for the 1–50 listing.

Live preview

Try integers in the safe JS range; uses a BigInt cube test (no math.h in the browser).

Fixes vs naive round

Avoid biased trunc(cbrt(n) + 0.5) on negative n; widen cubes with BigInt when needed.

Prerequisites

console.log, integer multiplication, and (for Example 1) Math.cbrt, Math.round, and BigInt for the final cube comparison.

  • Number.isSafeInteger for inputs that must stay exact in Number.
  • BigInt literals (3n) and exponentiation ** for exact cubes.

What is a perfect cube?

An integer n is a perfect cube (cube number) if n = k3 for some integer k. That includes 0 = 03 and negative examples such as -8 = (-2)3.

Do not confuse “cube number” with “multiple of 3” or “square”; the operation is cubing, not squaring.

8
27
9 not a cube

Cube root characterization

For real x, the equation k3 = x has a unique real cube root k. For integer n, n is a perfect cube iff that real root is an integer—equivalently some rounded float estimate k satisfies k3 = n once checked in exact integer arithmetic.

n = 27

Real cube root of 27 is 3, and 33 = 27.

Intuition

27
Check
3·3·3
28 Not a cube
Near
between 33 and 43

Takeaway: perfect cubes grow quickly in gaps: 1, 8, 27, 64, …

Live preview

JavaScript safe integers. Integer cube test: take sign, grow k on |n| with BigInt until k3 ≥ |n|, then compare.

Try -8, 0, or a non-cube such as 28.

Live result
Press “Check cube” to classify the number.

Algorithm

Goal: return true iff n = k3 for some integer k.

Floating route

Compute k = Math.round(Math.cbrt(n)), then verify BigInt(k) ** 3n === BigInt(n).

Integer route

For nonnegative n, increment k from 0 until k3 ≥ n; answer yes iff equality (widen with BigInt for large n).

📜 Pseudocode

Pseudocode (nonnegative integer route)
function isPerfectCubeNonneg(n):  // n ≥ 0
    k ← 0
    while k * k * k < n:
        k ← k + 1
    return k * k * k = n
1

Math.cbrt + Math.round + exact cube check

Math.round avoids naive trunc(cbrt(n) + 0.5) bugs on negative cubes such as -27. The cube is checked with BigInt so the product never silently overflows a Number.

JavaScript
function isCube(number) {
  const k = Math.round(Math.cbrt(number));
  return BigInt(k) ** 3n === BigInt(number);
}

const inputNumber = 27;

if (isCube(inputNumber)) {
  console.log(inputNumber + " is a cube number.");
} else {
  console.log(inputNumber + " is not a cube number.");
}
Try it Yourself

Explanation

Math.cbrt returns an IEEE double; for integers within Number.MAX_SAFE_INTEGER, Math.round usually recovers the true integer root, and BigInt confirms it. For integers beyond exact double range, prefer the pure integer route.

BigInt(k) ** 3n === BigInt(number)

Verify in integers. Confirms the rounded root truly cubes back to number.

2

Integer scan: cubes from 1 to 50

Same output as the reference: 1 8 27. Uses nonnegative k only; BigInt widening matches the C long long cast pattern for larger num.

JavaScript
function isCubeNumber(num) {
  let k = 0;

  while (BigInt(k) * BigInt(k) * BigInt(k) < BigInt(num)) {
    k++;
  }

  return BigInt(k) ** 3n === BigInt(num);
}

console.log("Cube numbers in the range 1 to 50:");

let line = "";
for (let i = 1; i <= 50; i++) {
  if (isCubeNumber(i)) {
    line += i + " ";
  }
}
console.log(line.trim());
Try it Yourself

Explanation

The loop finds the smallest k with k3 ≥ num. If num is a perfect cube, equality holds; otherwise k3 overshoots.

BigInt(k) * BigInt(k) * BigInt(k)

Widen before multiply so intermediate products stay defined for larger inputs.

Optimization

Binary search on k. For huge nonnegative n, binary-search k in [0, n] (or a tighter upper bound) instead of stepping by one.

No Math. Newton’s method on f(k)=k3-n can approximate k, but you still need an exact integer final check.

Interview: mention sign, 0, overflow, and why you verify k3 in integers after any float step.

❓ FAQ

An integer n is a perfect cube if n = k^3 for some integer k. Examples: 0 = 0^3, 1 = 1^3, 8 = 2^3, 27 = 3^3, -8 = (-2)^3.
Biasing toward +infinity before truncating toward zero breaks several negative half-integer cases. Use Math.round(Math.cbrt(n)) (or integer search), then verify k^3 === n in exact arithmetic (e.g. BigInt).
In theory yes near huge integers; for typical Number-safe integers, Math.round(Math.cbrt(n)) plus a final BigInt cube check is reliable. For maximal safety, use only integer arithmetic (Example 2 style) or BigInt throughout.
For large n, k^3 can exceed Number.MAX_SAFE_INTEGER before you compare. Example 1 verifies with BigInt(k)**3n; Example 2 widens with BigInt when scanning larger ranges.
Yes: 0 = 0^3. The programs on this page treat 0 as a perfect cube.
Increasing k until k^3 >= |n| runs in O(|n|^{1/3}) iterations for nonnegative n; each step is O(1) with BigInt arithmetic for the cube.

🔄 Input / output examples

Swap inputNumber in Example 1 or extend the loop in Example 2.

nPerfect cube?k
27Yes3
-8Yes-2
28No
0Yes0

Edge cases and pitfalls

Rounding cube roots in floating point without a final integer check is fragile; naive +0.5 truncation is wrong for several negative values.

Sign

Math.cbrt on negatives

Math.cbrt is defined for negative reals. Pair it with Math.round, not biased-half truncation via + 0.5 and Math.trunc.

Zero

n = 0

Perfect cube: 03. The integer loop must start at k = 0 (Example 2 already does when reused for nonnegative num).

Overflow

k * k * k in Number

For large k, the product can exceed Number.MAX_SAFE_INTEGER before comparison. Use BigInt for the cube, as in the samples.

Double

Huge integers

IEEE double cannot represent all integers past 253 exactly; stick to integer or BigInt methods for big integers.

⏱️ Time and space complexity

MethodTimeExtra space
Math.cbrt + verifyO(1) typical library costO(1)
Linear k scan (nonnegative)O(n1/3) iterationsO(1)
Binary search on kO(log n) iterationsO(1)

Here n denotes the magnitude of the input for the scan bound k ≈ n1/3.

Summary

  • Definition: n = k3 for some integer k (includes 0 and negatives).
  • Code: Math.round(Math.cbrt(n)) then BigInt cube-check, or pure integer increment / binary search.
  • Watch-outs: negative rounding, overflow in k3, Number precision limits.
Did you know?

A nonzero integer n is a perfect cube iff in its prime factorization every exponent is a multiple of three. The cube roots of 0 and 1 are 0 and 1 themselves.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful