## DEV Community is a community of 699,738 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Easy peasy perfect square

Divyajyoti Ukirde
I'm currently ðŸŒ± learning and ðŸ”­ working on the Javascript Ecosystem. ðŸ˜„ Pronouns: she/her âš¡ Fun fact: I have a Computer Engineering background but the most credit goes to Google and Stack Overflow

The question is to return true if the number is perfect square otherwise false.
A perfect square is an integer that is the square of an integer.
Javascript provides numerous ways to write a code for this. I will let you know that this is what I did:

``````const isSquare = (n) => {
return  (Math.sqrt(n) === Math.ceil(Math.sqrt(n)));
}
``````

Below is the solution that impressed me, because it is simple Math my brain couldn't get hold of first:

``````const isSquare = function(n){
return Math.sqrt(n) % 1 === 0;
}
``````

And it is clever and also follows the best practices!

Checking the datatype would also work using `isInteger`. But you never know when it would get obsolete! Languages change but the Math around remains the same! Choose better!

## Discussion (4)

Saurabh Sharma • Edited

I used Number.isInteger detect if the square is integer github.com/itsjzt/euler-project/bl...

Divyajyoti Ukirde • Edited

That is very much correct. It shows your knowledge in the language is really good. Also, it is always better to know the underlying operations. :)

Saurabh Sharma

It shows your knowledge in the language is really good

Stack overflow