Check Perfect Square in JavaScript

Beginner
⏱️ 10 min read
📚 Updated: May 2026
🎯 2 Code Examples
Number theory

What you’ll learn

  • What a perfect square is with quick examples (1, 4, 9, 16...).
  • How to test a value with an integer loop using i * i.
  • How to test with Math.sqrt and list squares in a range.
  • A live preview and common edge cases (0, negatives, large values).

Overview

A nonnegative integer n is a perfect square if there exists integer k such that k * k = n. The page shows both loop and square-root approaches.

Two programs

Loop check for one value and sqrt-based listing for a range.

Live preview

Try values like 15, 16, 0 and 49 instantly.

Interview-ready

Know loop logic, sqrt shortcut, and corner cases.

Prerequisites

Basic JavaScript loops and integer arithmetic.

  • Functions, loops, and conditionals.
  • Understanding squares like 3 * 3 = 9.

What is a perfect square?

A perfect square is any nonnegative integer that can be written as k * k for integer k.

Examples: 0, 1, 4, 9, 16, 25. Non-examples: 2, 3, 5, 6, 7, 8.

Quick examples

16Square
Because
4 * 4 = 16
15Not a square
Because
Between 9 and 16
1Square
Because
1 * 1 = 1

Live preview

Checks if n has integer root k with k * k === n.

Use whole numbers n >= 0. Very large values are capped.

Live result
Press “Run check” to see whether n is a perfect square.

Algorithm

Goal: decide whether nonnegative integer n equals k * k for some integer k.

Loop method

Try increasing i while i * i <= n; return true on exact match.

sqrt method

Let r = Math.floor(Math.sqrt(n)); perfect if r * r === n.

📜 Pseudocode

Pseudocode
if n < 0: false
i = 0
while i*i <= n:
  if i*i == n: true
  i++
false
1

Loop-based check

Pure integer method without relying on floating-point square root.

JavaScript
function isPerfectSquare(number) {
  if (number < 0) return false;

  for (let i = 0; i * i <= number; i++) {
    if (i * i === number) {
      return true;
    }
  }
  return false;
}

const testNumber = 16;
if (isPerfectSquare(testNumber)) {
  console.log(testNumber + " is a perfect square.");
} else {
  console.log(testNumber + " is not a perfect square.");
}
Try it Yourself
2

Using Math.sqrt in a range

Short and practical for listing squares in a fixed interval.

JavaScript
function isPerfectSquare(num) {
  if (num < 0) return false;
  const root = Math.floor(Math.sqrt(num));
  return root * root === num;
}

const values = [];
for (let i = 1; i <= 50; i++) {
  if (isPerfectSquare(i)) {
    values.push(String(i));
  }
}

console.log("Perfect Squares in the Range 1 to 50:");
console.log(values.join(" "));
Try it Yourself

Notes

Floating limits. For huge integers, Math.sqrt can be affected by precision. Verify with root * root === n.

Overflow safety. In loop checks, guard multiplication if you later target very large integer domains.

Zero and one. Both are perfect squares and should return true.

❓ FAQ

A whole number is a perfect square if it equals k * k for some whole number k. Example: 9 = 3 * 3.
Yes. 1 = 1 * 1.
Yes. 0 = 0 * 0.
The loop uses only integer arithmetic and is easy to reason about. sqrt is shorter but you should still verify with root * root === n.
Not over real integers. For this tutorial, negatives return false.
Loop method is O(sqrt(n)) for one value. sqrt-based check is O(1) for one value.

🔄 Input / output examples

ValueTypical output
1616 is a perfect square.
1515 is not a perfect square.
11 is a perfect square.
00 is a perfect square.

Edge cases

n = 0

Zero

0 = 0 * 0, so it is a perfect square.

Negative n

Not a square in reals

For this tutorial, negatives return false.

sqrt rounding

Always re-verify

Use root * root === n rather than trusting sqrt output alone.

⏱️ Time and space complexity

ApproachTime (single n)Extra space
Loop until i * i > nO(sqrt(n))O(1)
Math.sqrt checkO(1)O(1)
Range scan [1..U]O(U) checksO(1)

Summary

  • Rule: perfect square iff n = k * k for integer k.
  • Methods: integer loop or Math.sqrt with verification.
  • Edge care: handle 0/1 correctly and reject negatives.
Did you know?

A perfect square is a count you can arrange into a square grid with equal rows and columns. Consecutive squares differ by odd numbers: 1, 4, 9, 16 ... gaps 3, 5, 7 ...

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.

9 people found this page helpful