Check if the Number is Positive, Negative or Zero

This is a preview lesson
Register or sign in to take this lesson.

Positive, Negative or Zero

This tutorial shows a snippet to find if the given number is positive, negative or zero.

Conditions:
For zero: If the given number is equal to 0 than the given number is Zero
For positive numbers:  If the number is greater 0 then the given number is Positive.
For negative numbers: If the number is less than 0 than the given number is Negative.

Pseudo-code

if(number equals 0) {
   print "The given number is zero"
}
else if (number is greater than 0) {
    print "The given number is positive"
}
else {
    print "The given number is negative'
}

JavaScript Syntax

if(number === 0) {
   console.log("The given number is zero");
}
else if (number > 0) {
    console.log("The given number is positive");
}
else {
    console.log("The given number is negative');
}

Firstly, let’s look at a example of it.

const number = 10;

if(number === 0) {
   console.log("The given number is zero");
}
else if (number > 0) {
    console.log("The given number is positive");
}
else {
    console.log("The given number is negative');
}

/*
 * Output
 * prints: The given number is positive  // Because 10 is a positive number  
*/

Now, the same code wrapped inside function looks like

// function that checks if the number is positive negative or zero

function positiveNegativeOrZero(number) {
    if(number === 0) {
       console.log("The given number is zero");
    }
    else if (number > 0) {
        console.log("The given number is positive");
    }
    else {
        console.log("The given number is negative');
    }
}

positiveNegativeOrZero(10);
/*
 * Output
 * prints: The given number is positive  // Because 10 is a positive number  
*/


positiveNegativeOrZero(0);
/*
 * Output
 * prints: The given number is zero  // Because 0 is zero  
*/

positiveNegativeOrZero(-10);
/*
 * Output
 * prints: The given number is negative  // Because -10 is a negative number  
*/