Check if a number is Negative or Positive in Javascript

In this post, I will show you how to write a simple JavaScript program to check if a number is Negative or Positive. I am going to show you two ways of doing it, one is the old way and then the ES6 way. So, let’s begin.

The Old Way:

If you notice in the above snippet, there is one special ‘if’ condition that checks the value ‘Zero’. Basically Zero is neither Positive nor Negative, so it is very important to put this condition.

The ES6 Way:

So basically, Math.sign() function returns either 1 or -1. Somethis like this,

Math.sign(10) returns 1
Math.sign(-10) return -1

Thanks for reading. Check my other blog posts as well to know more about JavaScript programming.

Related Posts

Leave a Reply