check if a number is positive or negative javascript code example
Example 1: check if number is negative javascript
const positive = 5;
const negative = -5;
const zero = 0;
Math.sign(positive);
Math.sign(negative);
Math.sign(zero);
Example 2: check if number is negative javascript
Math.sign() has 5 possible return values:
1
-1
0
-0
NaN
Example 3: Check if a Number is Positive or Negative using if else
public class PositiveNegative {
public static void main(String[] args) {
double number = 12.3;
if (number < 0.0)
System.out.println(number + " is a negative number.");
else if ( number > 0.0)
System.out.println(number + " is a positive number.");
else
System.out.println(number + " is 0.");
}
}