Signup/Sign In
PUBLISHED ON: MARCH 13, 2023

JavaScript Program to Check if a number is Positive, Negative, or Zero

JavaScript is a popular programming language that is used a lot for building websites and other things. It is a powerful language that lets developers make web apps that are both complex and interactive. In this tutorial, we'll learn how to write a JavaScript program that checks if a number is positive, negative, or zero. This can be a useful part of a program that works with numbers because it lets the program handle different situations and act in the right way based on the number's value. We'll use conditional statements and simple math to figure out the sign of the number and show the user the right message.

By the end of this tutorial, you will have a good idea of how to use JavaScript to do simple math operations and make decisions based on the results. We also have an interactive JavaScript course where you can learn JavaScript from basics to advanced and get certified. Check out the course and learn more from here.

Approach

The main idea is to check whether the Given number is Positive Negative or Zero.

  • If the number is greater than 0, Positive.
  • If the number is less than 0, Negative.
  • If the number is equal to zero then Zero.

JavaScript Program to Check if a number is Positive, Negative, or Zero

// Program in JavaScript that checks if the number is positive, negative, or zero

const num = parseInt(prompt("Enter a number: "));

// check if num is greater than 0
if (number > 0) 
    console.log("The number is positive");


// check if num is less than 0
else if (number < 0) 
  console.log("The number is negative");


// if num is equal to 0
else 
     console.log("The number is zero");


Enter a number: 3
The number is positive.

Conclusion

In conclusion, the JavaScript program that we have developed successfully checks whether a given number is positive, negative, or zero. By using the conditional statement "if...else," we were able to determine the sign of the input number and output the appropriate result.

This program can be useful in a variety of applications where we need to perform basic arithmetic operations and make decisions based on the sign of a number. It is a simple but powerful tool that can help us to solve problems and write more efficient code.



About the author:
Proficient in the creation of websites. Expertise in Java script and C#. Discussing the latest developments in these areas and providing tutorials on how to use them.