JavaScript Program to Check if a Given Integer is Positive, Negative, or Zero
Last Updated :
03 Jun, 2024
In this article, we will discuss how we can determine whether the given number is Positive, Negative, or Zero. As we know if the number is less than 0 then it will be called a “Negative” number if it is greater than 0 then it will be called a “Positive” number. And if it doesn’t lie between them, then it will be 0.
Example:
Input: 10
Output: Positive number
Input: -12
Output: Negative number
Input: 0
Output: Zero
These are the following ways by which we can find out whether the number is Positive, Negative, or Zero:
Method 1: Using if-else statement
In this approach, we are using an if-else statement. in which whatever the condition will get satisfied and return true that code block will get executed and it will print the result in the console.
Example: It describes how we can find the given number is Positive, Negative, or Zero using an if-else statement
JavaScript
// Function for number checking
function NumberSignChecker(n) {
if (n > 0) {
console.log("Positive number");
} else if (n < 0) {
console.log("Negative number");
} else {
console.log("Zero");
}
}
// Variables holding values
let number1 = -5;
let number2 = 5;
let number3 = 0;
NumberSignChecker(number1); // Negative number
NumberSignChecker(number2); // Positive number
NumberSignChecker(number3); // Zero
OutputNegative number
Positive number
Zero
Time complexity: The time complexity of the above code is O(1), as it takes only one step to complete the operation.
Space complexity: The space complexity of the above code is O(1), as no extra space is required in order to complete the operation.
In this approach we are using ternary operator. Ternary operator is shorthand of “if-else” statement. If “n<0” is true then it will print “Negative” else it will check for another condition and it will print result accordingly.
Example: It describes how we can find the given number is Positive, Negative, or Zero using ternary operator
JavaScript
let n = 0;
// Using ternary operator
// for solving the
// problem
let ans = n < 0 ? "Negative" : n > 0 ? "Positive" : "Zero";
// Printing result in
// console
console.log(ans);
Time complexity: O(1) as it is doing constant operations
Space complexity: O(1) as it is using constant space for variables
In this method we are using switch case for solving this problem. Switch statement will check the condition and return the result. If any of case is equivalent to that result then that code block will get executed and it will print result in the console else default statement will get executed automatically.
Example: It describes how we can find the given number is Positive, Negative, or Zero using switch case
JavaScript
// JavaScript program for checking if number
// is positive, negative or zero
let num = 10;
// Switch-case statement
switch (num / num) {
case 1:
console.log("Positive nunber");
break;
case -1:
console.log("Negative number");
break;
case 0:
console.log("Zero");
break;
}
Time complexity: O(1), as it is doing constant operations
Space complexity: O(1), as it is using constant space for variables
Using Math.sign()
In this approach we are using the Math.sign() method which returns the sign of a number as -1 for negative numbers, 0 for zero, and 1 for positive numbers. It checks the result of Math.sign(num) to determine the sign of the given number and returns the corresponding string.
Example: It describes how we can find the given number is Positive, Negative, or Zero using Math.sign() method.
JavaScript
function checkNumber(num) {
return Math.sign(num) === 1 ? "Positive" : Math.sign(num
) === -1 ? "Negative" : "Zero";
}
console.log(checkNumber(10));
console.log(checkNumber(-5));
console.log(checkNumber(0));
OutputPositive
Negative
Zero
Time complexity: O(1), as it is doing constant operations
Space complexity: O(1), as it is using constant space for variables
Method 5: Using bitwise operators
In this approach, we leverage bitwise operators to check the sign of the integer. Specifically, we use the right shift operator (>>) to extract the sign bit and compare it with 0 to determine whether the number is positive, negative, or zero.
Example:
JavaScript
function checkNumber(num) {
if (num === 0) {
return "Zero";
} else if (num >> 31 !== 0) {
return "Negative number";
} else {
return "Positive number";
}
}
// Test cases
console.log(checkNumber(10));
console.log(checkNumber(-5));
console.log(checkNumber(0));
OutputPositive number
Negative number
Zero
Similar Reads
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 th
3 min read
JavaScript Program to Check if a Number is Sparse or Not
A number is considered sparse if it does not have any consecutive ones in its binary representation. Example: Input: n=21Output: TrueExplanation: there are no consecutive 1s in the binary representation (10101). Therefore, 21 is a sparse number.Input: n=22Output: FalseExplanation: there are consecut
3 min read
JavaScript Program to Find the Average of all Negative Numbers in an Array
Given an array of numbers, the task is to find the average of all negative numbers present in the array. Below are the approaches to find the average of all negative numbers in an array: Table of Content Iterative ApproachFunctional Approach (Using Array Methods)Using forEach with a Separate Functio
4 min read
JavaScript Program to Check Whether a Number is Perfect Square
The number that results from squaring another integer is called a perfect square. A perfect square is a number that can be expressed as the product of an integer with itself. There are several ways available in JavaScript to check whether a number is a perfect square or not which are as follows: Tab
4 min read
JavaScript Program to Print Negative Numbers in a Linked List
This JavaScript program aims to print negative numbers present in a linked list. A linked list is a data structure consisting of a sequence of elements, where each element points to the next element in the sequence. The program traverses the linked list and prints all the negative numbers encountere
2 min read
JavaScript Program to Print Positive Numbers in a Linked List
This JavaScript program aims to print positive numbers present in a linked list. A linked list is a data structure consisting of a sequence of elements, where each element points to the next element in the sequence. The program traverses the linked list and prints all the positive numbers encountere
2 min read
JavaScript Program to Check if a Number has Bits in an Alternate Pattern
JavaScript can be used to assess whether a given number follows an alternating pattern in its binary representation. By examining the binary digits of the number, one can determine if the sequence alternates between 0s and 1s, aiding in understanding the binary structure of the input. Examples: Inpu
2 min read
JavaScript Program to Count Even and Odd Numbers in an Array
In this article, we will write a program to count Even and Odd numbers in an array in JavaScript. Even numbers are those numbers that can be written in the form of 2n, while Odd numbers are those numbers that can be written in the form of 2n+1 form. For finding out the Even and Odd numbers in an arr
4 min read
JavaScript Program to find All Divisors of a Natural Number
In this article, we are going to implement a program through which we can find the divisors of any natural number. Divisors refer to the numbers by which we can divide another number, resulting in zero remainder. These divisors are also commonly referred to as factors. Example: Input: 14Output: 1,2,
2 min read
JavaScript Program to Multiply the Given Number by 2 such that it is Divisible by 10
In this article, we are going to implement a program through which we can find the minimum number of operations needed to make a number divisible by 10. We have to multiply it by 2 so that the resulting number will be divisible by 10. Our task is to calculate the minimum number of operations needed
3 min read