Problem Statement
given a number, write a JavaScript program to check if the number is negative
Solution
a number is negative if it is less than 0
. to check if given number is negative, we have to check if it is less than 0
. we can use comparison less than operator to check if given number is less than 0
Program
1. given number is n
. check if n
is negative
let n = -4;
if ( n < 0 ) {
console.log('negative number');
} else {
console.log('not a negative number');
}
we have used the condition in if-else statement
2. positive number in n
let n = 6;
if ( n < 0 ) {
console.log('negative number');
} else {
console.log('not a negative number');
}