JavaScript Code


How to check if number is zero in JavaScript?


Check if number is zero in JavaScript

In this tutorial, you are given a number. Write a JavaScript program to check if the number is zero 0.

Solution

A number is zero if it is equal to 0. To check if given number is zero, we can use comparison equal-to operator to check if given number is equal to 0.

Program

1. In the following program, we are given a number in n. We shall check if n is zero.

let n = 0;
if ( n == 0 ) {
    console.log('zero');
} else {
    console.log('not a zero');
}

We have used the condition as a boolean expression in if-else statement.

2. Now, we shall take a non-zero number in n, and programmatically check if value in n is zero or not.

let n = 7;
if ( n == 0 ) {
    console.log('zero');
} else {
    console.log('not a zero');
}