Examples
- JavaScript Program to Check if a Number is Positive, Negative, or Zero
- JavaScript Program to Find the Largest Among Three Numbers
- JavaScript Program to Check Prime Number (4 Ways)
- JavaScript Program to Find the Factorial of a Number
- Armstrong Number in JavaScript (6 Programs)
- JavaScript Program to Find HCF or GCD
- JavaScript Program to Find LCM (5 Ways)
- JavaScript Program to Convert Decimal to Binary
JavaScript Program to Check if a Number is Positive, Negative, or Zero
Checking if a number is positive, negative, or zero is a fundamental exercise for beginners learning JavaScript. This program walks you through accepting user input, applying conditional logic, and displaying the result.
Mastering this skill not only helps you grasp conditional statements but also builds a strong base for tackling more complex programming tasks in the future.
JavaScript Program to Check if a Number is Positive, Negative, or Zero Using if-else Statements
The if-else conditional statement is one of the simplest and most intuitive ways to perform this check.
Code
function checkNumber(num) {
if (num > 0) {
console.log(`${num} is Positive`);
} else if (num < 0) {
console.log(`${num} is Negative`);
} else {
console.log(`${num} is Zero`);
}
}
// Example Usage
checkNumber(5);
checkNumber(-3);
checkNumber(0);
Output
5 is Positive
-3 is Negative
0 is Zero
Explanation
-
If num is greater than 0, it is positive.
-
If num is less than 0, it is negative.
-
If neither condition is true, the number is zero.
JavaScript Program to Check if a Number is Positive, Negative, or Zero Using Ternary Operators
Ternary operators provide a concise alternative to if-else statements.
Code
function checkNumber(num) {
let result = num > 0
? `${num} is Positive`
: (num < 0
? `${num} is Negative`
: `${num} is Zero`);
console.log(result);
}
// Example Usage
checkNumber(10);
checkNumber(-7);
checkNumber(0);
Output
10 is Positive
-7 is Negative
0 is Zero
Explanation
-
The ternary operator checks the condition num > 0 first. If true, it returns "Positive".
-
If false, it checks num < 0. If true, it returns "Negative".
-
If both conditions are false, it defaults to "Zero".
JavaScript Program to Check if a Number is Positive, Negative, or Zero Using Math.sign() Method
The Math.sign() method is a quick and direct way to determine the sign of a number. It simplifies checking for positive, negative, or zero by returning specific values for each case.
Code
function checkNumber(num) {
let sign = Math.sign(num);
if (sign === 1) {
console.log(`${num} is Positive`);
} else if (sign === -1) {
console.log(`${num} is Negative`);
} else {
console.log(`${num} is Zero`);
}
}
// Example Usage
checkNumber(15);
checkNumber(-20);
checkNumber(0);
Output
15 is Positive
-20 is Negative
0 is Zero
Explanation
-
Math.sign(num) directly evaluates the sign of a number.
-
The return value of 1 indicates positive, -1 indicates negative, and 0 indicates zero.
JavaScript Program to Check if a Number is Positive, Negative, or Zero Comparison Using Switch-Case
Switch-case is another method, although less common for this scenario.
Code
function checkNumber(num) {
switch (true) {
case num > 0:
console.log(`${num} is Positive`);
break;
case num < 0:
console.log(`${num} is Negative`);
break;
default:
console.log(`${num} is Zero`);
}
}
// Example Usage
checkNumber(8);
checkNumber(-5);
checkNumber(0);
Output
8 is Positive
-5 is Negative
0 is Zero
Explanation
-
The switch(true) allows each case to evaluate a condition.
-
The case num > 0 checks if the number is positive.
-
The case num < 0 checks if the number is negative.
-
The default handles all other scenarios, which in this case is zero.