- Because
- 4 * 4 = 16
Check Perfect Square in JavaScript
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.sqrtand 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
- Because
- Between 9 and 16
- Because
- 1 * 1 = 1
Live preview
Checks if n has integer root k with k * k === n.
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
if n < 0: false
i = 0
while i*i <= n:
if i*i == n: true
i++
falseLoop-based check
Pure integer method without relying on floating-point square root.
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.");
}Using Math.sqrt in a range
Short and practical for listing squares in a fixed interval.
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(" "));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
🔄 Input / output examples
| Value | Typical output |
|---|---|
| 16 | 16 is a perfect square. |
| 15 | 15 is not a perfect square. |
| 1 | 1 is a perfect square. |
| 0 | 0 is a perfect square. |
Edge cases
n = 0Zero
0 = 0 * 0, so it is a perfect square.
Not a square in reals
For this tutorial, negatives return false.
Always re-verify
Use root * root === n rather than trusting sqrt output alone.
⏱️ Time and space complexity
| Approach | Time (single n) | Extra space |
|---|---|---|
Loop until i * i > n | O(sqrt(n)) | O(1) |
| Math.sqrt check | O(1) | O(1) |
| Range scan [1..U] | O(U) checks | O(1) |
Summary
- Rule: perfect square iff
n = k * kfor integer k. - Methods: integer loop or
Math.sqrtwith verification. - Edge care: handle 0/1 correctly and reject negatives.
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 ...
9 people found this page helpful
