We are going to learn two ways to check if variable is equal to 0 using JavaScript. In this article we will use the if statement and a ternary operator to do that. Let’s go into detail now.
Check if variable is equal to 0 using JavaScript
Using the if statement
The if statement is well-known by most programmers in terms of comparing variables’ values. And the strict equal operator is commonly used inside the if statement whenever they have to check whether a variable is equal to a number or not.
If you want to check its type and make sure a variable is a number, not a string that is “0”, you should use the strict equal operator (===):
let variable = '0' // Check if variable is equal to 0 using JavaScript using === if (variable===0) console.log(variable + " is equal to 0") else console.log(variable + " is NOT equal to 0")
Output:
0 is NOT equal to 0
The above example uses the === operator to compare the variables with a zero number in terms of type and value. However, the normal equality operator will check the value only but not the type:
let variable = '0' // Check if variable is equal to 0 using JavaScript using == if (variable==0) console.log(variable + " is equal to 0") else console.log(variable + " is NOT equal to 0")
Output:
0 is equal to 0
So what is the difference? The strict equality will make sure the type of the variable is also the same as the value you have passed to check. This means if either of the operands to check does not have the same type, such as numbers vs strings, then it will return false immediately. Because, at first, they are not even equal in type (numbers are not string type). However, the normal operator doesn’t check type equality, so as a result, we will get the output in this example:
let variable = '000' // Check if variable is equal to 0 using JavaScript using === if (variable==0) console.log(variable + " is equal to 0") else console.log(variable + " is NOT equal to 0")
Output:
000 is equal to 0
Now we have seen the difference between each operator to decide using which operator depends on what you actually expect to receive.
Using ternary operator
The first method will help you check if a variable is equal to 0 using the if-else statement. As you can see, it may consume at least four lines of code to tackle this, however, you can reduce them to one line by using the ternary operator, for example:
let variable = '000' // Check if variable is equal to 0 using JavaScript using === console.log(variable + (variable===0?" equals to 0":" does not equal to 0"))
Output:
000 does not equal to 0
This solution is simpler than the previous one. However, you must put the ternary operator inside the parenthesis to work. As you can see, this approach also produces the same result because we have used a strict equal operator (===) to ensure it is equal in both value and type. If you just want to check the value only, you can change the === operator to == instead. Therefore, we successfully check if a variable is equal to 0 using JavaScript.
Summary
We have discussed two different ways to check if variable is equal to 0 using JavaScript. It would be helpful if you considered that the second approach will optimize your code and work fine. If you need more articles about JavaScript, you can look them up here.
Maybe you are interested:
- Check if Variable Doesn’t Equal Multiple Values in JavaScript
- Check if a variable is a string using javascript
- Check if a variable is defined or initialized in javascript

I’m Edward Anderson. My current job is as a programmer. I’m majoring in information technology and 5 years of programming expertise. Python, C, C++, Javascript, Java, HTML, CSS, and R are my strong suits. Let me know if you have any questions about these programming languages.
Name of the university: HCMUT
Major: CS
Programming Languages: Python, C, C++, Javascript, Java, HTML, CSS, R