How to use if-else Statements In Javascript
In this approach we are using the if-else or conditional statement will perform some action for a specific condition. If the condition meets then a particular block of action will be executed otherwise it will execute another block of action that satisfies that particular condition.
Syntax:
if (condition) {
// Executes this block if
// condition is true
}
else {
// Executes this block if
// condition is false
}
Example: In this example, we are using the above-explained approach.
const number = prompt("Enter a number");
if (number > 0) {
console.log("The number is positive");
}
else if (number < 0) {
console.log("The number is negative");
}
else {
console.log("The number is zero");
};
Output:
JavaScript Program to Check if a number is Positive, Negative, or Zero
In this article, we are going to learn if a number is positive, negative, or zero, for numerous mathematical operations and conditional statements in JavaScript. It is critical to know if a given number is positive, negative, or zero. This article provides a straightforward approach in JavaScript that lets you determine whether a given number belongs to one of these groups.
Several methods can be used to Check if a number is Positive, Negative, or Zero.
We will explore all the above methods along with their basic implementation with the help of examples.
Table of Content
- Using Switch Statement
- Using if-else Statements
- Using Ternary Operator
- Using Math.abs()