DEV Community

Cover image for Math.sign: How to Check if a Number is Positive or Negative in JavaScript

Math.sign: How to Check if a Number is Positive or Negative in JavaScript

Samantha Ming on April 07, 2020

Determining the sign of a number is super easy now with ES6's Math.sign ๐Ÿ‘ It will indicate whether the number is positive, negative or zero. co...
Collapse
 
fluffynuts profile image
Davyd McColl • Edited

slight error:

Math.sign either returns 1 or -1, so:

if (Math.sign(number)) {
  // always run
} else {
  // never run
}

did you perhaps mean if (Math.sign(number) > 0) ? in which case if (number > 0) is more concise.

Collapse
 
samanthaming profile image
Samantha Ming

OH good catch, let me fix it! Thanks for letting me know ๐Ÿ˜ฑ

Collapse
 
dharanr profile image
Dharan

Its crazy to know, that even a positive / negative one line execution needs this much knowledge LoL

Collapse
 
samanthaming profile image
Samantha Ming

hahahaha ๐Ÿ˜‚Tip of the iceberg...always more to know and more to learn when it comes to javascript ๐Ÿ˜†

Collapse
 
ponyjackal profile image
ponyjackal

Gotcha!

Collapse
 
basarozcan profile image
BaลŸar ร–zcan

you always find a good topic, thank you

Collapse
 
samanthaming profile image
Samantha Ming • Edited

thank you! i just pick topics that i want to learn, luckily this one resonates with you ๐Ÿ˜„And if there is a topic you want me to cover, please let me know! ๐Ÿ‘

Collapse
 
hafizpustice05 profile image
hafizpustice05

Great content.

Collapse
 
samanthaming profile image
Samantha Ming

Thanks! Glad you liked the article ๐Ÿ˜„

Collapse
 
aralroca profile image
Aral Roca

+10 years with JavaScript and I never listen about negative 0! But make sense...

Collapse
 
samanthaming profile image
Samantha Ming

I only learned the negative 0 when i wrote this article. It will be interesting to see when i can actually apply it. I'll make sure to share when that happens ๐Ÿ˜Š

Collapse
 
laradurrant profile image
Lara Durrant

Thanks for the article! Itโ€™s always a good reminder that thereโ€™s probably a built in JavaScript method for pretty much anything you wanna do! :)

Collapse
 
samanthaming profile image
Samantha Ming

Totally! JS is a big toolbox and continues to add more tools for us to use, which is awesome. Thanks for reading my article ๐Ÿ‘

Collapse
 
somedood profile image
Basti Ortiz

You learn something new everyday. Thanks for sharing!

Collapse
 
samanthaming profile image
Samantha Ming

I like that -- keep that student mindset so we can continue to learn and grow everyday! Thanks for reading ๐Ÿ‘

Collapse
 
bennypowers profile image
Benny Powers ๐Ÿ‡ฎ๐Ÿ‡ฑ๐Ÿ‡จ๐Ÿ‡ฆ

Love your posts, Samantha, keep them coming

Collapse
 
samanthaming profile image
Samantha Ming

you bet! thanks for the encouragement benny ๐Ÿ˜Š

Collapse
 
jack_garrus profile image
Nadia Guarracino • Edited

Checks browser support
Be happy that is wide supported
Read the IE line
Laugh with sadness

By the way, cool tip!

Collapse
 
samanthaming profile image
Samantha Ming

Laugh with sadness -- a perfect description of IE ๐Ÿ˜ต

Collapse
 
miteshkamat27 profile image
Mitesh Kamat

Good to use !!