How to Check if a Number is Positive or Negative in JavaScript
Validating numbers is a fundamental task in programming. You often need to determine if a value is positive, negative, or zero, and whether it's an integer or a floating-point number. JavaScript provides simple and effective tools for these checks, primarily using standard comparison operators and built-in Math and Number methods.
This guide will teach you the best practices for checking a number's sign. You will learn the difference between a strict type check and a more lenient check that allows for numeric strings, and how to specifically validate for integers.
The Core Method: Comparison Operators (> and <)
For most use cases, the simplest and most readable way to check if a number is positive or negative is with the standard greater-than (>) and less-than (<) operators.
The logic:
- A number is positive if it is
> 0. - A number is negative if it is
< 0.
An important feature of these operators is that they perform type coercion. If you compare a numeric string like "5", JavaScript will automatically convert it to a number before the comparison.
For example:
function isPositive(value) {
return value > 0;
}
function isNegative(value) {
return value < 0;
}
// Example Usage:
console.log(isPositive(10)); // Output: true
console.log(isPositive('10')); // Output: true (due to type coercion)
console.log(isPositive(-10)); // Output: false
console.log(isPositive('abc')); // Output: false (coerces to NaN)
console.log(isNegative(-5)); // Output: true
console.log(isNegative('-5')); // Output: true
An Alternative Method: Math.sign()
The Math.sign() method provides a more descriptive way to determine a number's sign. It returns one of five possible values, which can be very useful for switch statements or more complex conditional logic.
1: if the number is positive.-1: if the number is negative.0: if the number is0.-0: if the number is-0.NaN: if the number is not a number.
Like the comparison operators, Math.sign() also coerces its input into a number.
For example:
function isPositive(value) {
return Math.sign(value) === 1;
}
function isNegative(value) {
return Math.sign(value) === -1;
}
// Example Usage:
console.log(isPositive(50)); // Output: true
console.log(isPositive('50')); // Output: true
console.log(isNegative(-50)); // Output: true
console.log(isNegative('abc'));// Output: false (Math.sign returns NaN)
How to Check for Integers (Positive or Negative)
Sometimes you need to be more specific and check if a value is not just positive or negative, but also a whole number (an integer). For this, you should combine a comparison check with the Number.isInteger() method.
Crucially, Number.isInteger() is strict. It does not perform type coercion and will return false for numeric strings.
For example:
function isPositiveInteger(value) {
return Number.isInteger(value) && value > 0;
}
function isNegativeInteger(value) {
return Number.isInteger(value) && value < 0;
}
// Example Usage:
console.log(isPositiveInteger(10)); // Output: true
console.log(isPositiveInteger(10.5)); // Output: false
console.log(isPositiveInteger('10')); // Output: false (isInteger is strict)
console.log(isNegativeInteger(-5)); // Output: true
console.log(isNegativeInteger(-5.5)); // Output: false
Important Distinction: Strict vs. Coercive Checks
As shown, the simple comparison (> 0) and Math.sign() methods are convenient because they work on numeric strings. However, in some cases, you may want to ensure a value is strictly of the number data type.
Problem: the function should only accept 5, not "5".
function isPositive(value) {
return value > 0;
}
isPositive('5'); // Returns true, which may not be what you want.
Solution: to enforce a strict type check, add a typeof guard to your function.
function isStrictlyPositive(value) {
return typeof value === 'number' && value > 0;
}
// Example Usage:
console.log(isStrictlyPositive(10)); // Output: true
console.log(isStrictlyPositive('10')); // Output: false
This pattern provides the highest level of type safety.
Conclusion
JavaScript offers several straightforward ways to check if a number is positive or negative, and it's important to choose the right tool for your needs.
- For simple, lenient checks that allow for numeric strings, use the comparison operators
value > 0orvalue < 0. - For more descriptive logic,
Math.sign(value)provides a clear indication of the number's sign (1,-1, or0). - To validate for whole numbers, combine a comparison with
Number.isInteger(value). - For strict type safety, add a
typeof value === 'number'guard to your checks.