How to use Math.abs() In Javascript

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.

Output
The number is positive.
The number is negative.
The number is zero.




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()

Similar Reads

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....

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....

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....

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....