JavaScript Program to Check if a number is Positive, Negative, or Zero
Last Updated :
06 Aug, 2024
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.
Using Switch Statement
In this approach, we are using a switch statement to check a number's sign (positive, negative, or zero) based on Math.sign() method, this method is used to know the sign of a number, indicating whether the number specified is negative or positive.
Syntax:
switch (expression) {
case value1:
statement1;
break;
case value2:
statement2;
break;
. . .
case valueN:
statementN;
break;
default:
statementDefault;
}
Example: In this example, we are using the above-explained approach
JavaScript
function numberChecking(num) {
switch (Math.sign(num)) {
case 1:
console.log("The number is Positive");
break;
case -1:
console.log("The number is Negative");
break;
default:
console.log("The number is Zero");
}
}
numberChecking(12);
// Output: Positive
numberChecking(-1);
// Output: Negative
numberChecking(0);
// Output: Zero
OutputThe number is Positive
The number is Negative
The number is Zero
Using if-else Statements
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.
JavaScript
const number = 5;
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");
}
OutputThe number is positive
Using Ternary Operator
The ternary operator in JavaScript is an efficient one-line conditional statement that evaluates an expression and returns one of two defined values depending on a specific condition.
Syntax:
condition ? value if true : value if false
Example: In this example, we are using the above-explained approach.
JavaScript
function checkNumberSign(number) {
if (isNaN(number)) {
console.log("Invalid input. Please enter a valid number.");
} else {
const result =
number === 0 ? "The number is zero." :
number > 0 ? `${number} is positive.` : `${number} is negative.`;
console.log(result);
}
}
checkNumberSign(7);
Using Math.abs()
In this approach, we utilize the Math.abs() method to determine if a number is positive, negative, or zero. The Math.abs() method returns the absolute value of a number, which is its magnitude without regard to its sign. We can then compare the result with the original number to determine its sign.
Example: We will check if a number is positive, negative, or zero using the Math.abs() method.
JavaScript
function checkNumberSign(number) {
if (isNaN(number)) {
console.log("Invalid input. Please enter a valid number.");
} else {
if (number === 0) {
console.log("The number is zero.");
} else if (number === Math.abs(number)) {
console.log("The number is positive.");
} else {
console.log("The number is negative.");
}
}
}
// Test cases
checkNumberSign(5); // Output: The number is positive.
checkNumberSign(-7); // Output: The number is negative.
checkNumberSign(0); // Output: The number is zero.
OutputThe number is positive.
The number is negative.
The number is zero.
Similar Reads
JavaScript Program to Perform Simple Mathematical Calculation
In this article, we are going to learn how perform simple mathematical calculations by using JavaScript. In this, we will get the two numbers and one operator as input and then the program will calculate the result according to the provided inputs. Example 1: Input : Operation : '+' num1 : 5 num2 :
4 min read
Convert a negative number to positive in JavaScript
In this article, we will see how we can convert a negative number to a positive number in JavaScript by the methods described below. Below are the methods to convert a negative number to a positive in JavaScript: Table of Content Multiplying by -1Using Math.abs()adding a minus signFlipping the bitUs
4 min read
JavaScript Program to Count Positive and Negative Numbers in an Array
Given an array of numbers. Write a JavaScript program to count positive and negative numbers in an array.Example:Input: numbers_array1= [10,20, -1,22,99,20, -9]Output: positive no's=5, negative no's =2Input: numbers_array2= [-121, - 78, -13, 54, -23]Output: positive no's=1, negative no's=4Brute Forc
2 min read
JavaScript Program to Check if a Number is Odd or Even
We are going to learn how to check whether the number is even or Odd using JavaScript. In the number system, any natural number that can be expressed in the form of (2n + 1) is called an odd number, and if the number can be expressed in the form of 2n is called an even number.Even = {2k : k â Z}(whe
3 min read
Check if a number is positive or negative using if-else statement .
In this article, we will discuss how to check if a number is positive or negative with its working example in the R Programming Language using R if-else conditions. Checking whether a number is positive or negative is a fundamental operation in programming. It involves evaluating the sign of a numbe
3 min read
JavaScript Program to Check if a Number is Float or Integer
In this article, we will see how to check whether a number is a float or an integer in JavaScript. A float is a number with a decimal point, while an integer is a whole number or a natural number without having a decimal point. Table of ContentUsing the Number.isInteger() MethodUsing the Modulus Ope
2 min read
How to check whether a number is NaN or finite in JavaScript ?
When working with numbers in JavaScript, it's important to know how to determine if a value is NaN (Not-a-Number) or finite. This knowledge is crucial for data validation, error handling, and ensuring your code behaves as expected. In this article, we will see how to check whether the number is NaN
3 min read
Print all Positive Numbers in JavaScript Array
In JavaScript, arrays are the data structures used to store multiple elements. In some cases, we need to extract the elements that satisfy a specified condition. We can use a condition to extract all the positive numbers from an array with the below approaches. Example:Input: arr = [-1, -12, 35, -8,
3 min read
What are Positive and Negative Numbers?
Numbers are the mathematical values or figures used for the purpose of measuring or calculating quantities. It is also represented by numerals as 3, 4, 8, etc. Some other examples of numbers are whole numbers, integers, natural numbers, rational and irrational numbers, etc. The system includes diffe
4 min read
What is the difference between positive and negative numbers?
Algebra is a special branch of mathematics that is used to deal with the arithmetic operations, such as addition, subtraction, multiplication, or division, and the associated symbols, also known as variables. The variables are not fixed and change their value. Some of the examples of variables are x
3 min read