JavaScript

Check if the number is Not Greater than 0 in JavaScript

Sometimes, programmers need to determine whether a given number is greater than zero. To do so, in JavaScript, use conditional operators, such as less than equal to (<=) or greater than (>), with the NOT (!) operator.

This blog post will define the process to determine whether the number is not greater than 0 in JavaScript.

How to Check if the Number is not Greater than 0 in JavaScript?

To verify the number is greater than 0, use the “NOT” (!) operator with a conditional operator greater than, or the less than equal conditional operator is used in JavaScript. The NOT (!) operator can also be utilized to negate the result, it gives “true” if the given number is not greater than 0, else, “false” is returned.

To determine whether a number is not greater than 0, use the less than equal to “<=” operator. If the given number is not larger than zero, its outputs “true”.

Example 1: Determine Whether the Number is not Greater than 0 Using NOT (!) Operator
Create a variable “num” and assign a value “8”:

var num = 8;

Check the number greater than “0” using the greater than operator (“>”) and then negate it with the help of the not(“!”) operator. If it gives “false”, it means the number is greater than zero; if it outputs “true”, it means the number is not greater than zero:

console.log(!(num > 0));

The output shows “false”, which signifies that the number “8” is greater than zero:

Set the value of a variable to “-2”:

var num = -2;

Check whether “-2” is greater than zero or not using the NOT (!) operator:

console.log(!(num > 0));

The output displays “true”, which means the number “-2” is not greater than zero:

Example 2: Determine Whether the Number is not Greater than 0 Using less than equal to “<=” Operator
Here, in the following example, we will use the less than equal to “<=” operator to check if the number entered by the user is greater than zero or not.

First, create a button in the HTML file that will prompt the user to get the number by clicking on the button. Then, attach an “onclick” event to the button that will trigger while on the click event occurs:

<button onclick = "negNumber()">Click to Check for a Number</button>

In the JavaScript file or the <script> tag, use the following lines of code:

function negNumber(){
 let get = prompt("Enter a number:")
 if(get <= 0){
  alert("The number is not greater than 0")
 }
 else{
  alert("The number is greater than 0")
 }
}

In the above code snippet:

  • Define a function named “negNumber()”.
  • Utilize the “prompt()” method to obtain the value from the user.
  • Check the condition using a conditional statement and the operator; if the entered value is less than or equal to 0, display an alert message that the number is not greater than 0.
  • In the else statement, show an alert message that the specified number is larger than 0.

The corresponding output will be as follows:

We have gathered all the essential instructions related to verifying whether the number is greater than 0 or not using JavaScript.

Conclusion

For verifying, if the number is not greater than 0, use the “NOT” (“!”) operator with the conditional operator greater than “>” or the less than equal to “<=” conditional operator. Both approaches give a boolean value “true” or “false” when the number is greater or not greater than zero. This blog post defined the process to determine whether the number is not greater than 0 in JavaScript.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.