JavaScript

Check if a Value is Not Equal to 0 Using JavaScript

The mathematical computations in JavaScript often involve an exception to avoid the “0” values for getting the precise calculation. For instance, avoiding a particular calculation from being “nullified” or returning a garbage value. In such case scenarios, checking if a value is not equal to 0 using JavaScript is a very convenient approach to minimizing the chance of encountering an error.

This tutorial will discuss the approaches to check if a value is not equal to 0 using JavaScript.

How to Check if a Value is Not Equal to 0 in JavaScript?

To check if a value is not equal to 0 using JavaScript, the following approaches can be applied in combination with the “comparison” operators:

  • if/else” condition.
  • Ternary” operator.

Let’s discuss each of the approaches one by one!

Approach 1: Check if a Value is Not Equal to 0 in JavaScript Using if/else Condition

The “comparison” operator (==) is used to check if the two operands are equal or not, and the “if/else” condition checks for the specified condition. These approaches can be applied in combination to apply a condition upon the specified or user-defined value and display the corresponding message.

Example 1: Check if the Specified Value is Not Equal to 0
In this example, the specified value will be checked for the stated requirement:

<script type="text/javascript">
let value = 0;
if (value != 0) {
 console.log('The value is not zero');
}
else {
 console.log('The value is zero');
}
</script>

Perform the following steps as given in the above lines of code:

  • In the first step, specify the stated value that needs to be checked.
  • After that, apply the “if/else” condition along with the “comparison” operator(==) to check if the particular value equals “0”.
  • If so, the stated statement in the “if” condition will be logged on the console. Otherwise, the “else” condition will come into effect.

Output

In the above output, it can be observed that the applied condition works properly, referring to the specified value.

Example 2: Check if the User Entered Value is Not Equal to 0
This example will illustrate the stated requirement with the help of a user-defined value:

</div>
<script type="text/javascript">
let get = prompt("Enter the value:")
if (get == 0) {
 console.log('The value is zero');
}
else {
 console.log('The value is not zero');
}
</div>
</script>

In the above code snippet:

  • Firstly, input the value from the user to be checked if it equals “0” or not.
  • Now, repeat the steps discussed in the previous example for applying a condition upon the user-defined value with the help of the “if/else” condition and the “comparison” operator(==).
  • Finally, display the corresponding message referring to the redirected condition.

Output

From the above output, it is evident that both of the specified conditions work properly.

Approach 2: Check if a Value is Not Equal to 0 in JavaScript Using Ternary Operator

The “ternary” operator is a conditional operator having the same functionality as “if/else”. This operator can be implemented to apply a condition upon the specified value and return the corresponding output with the help of the “comparison” operator (!=).

Syntax

condition ? <expression> : <expression>

In the above syntax:

  • The former expression represents the “true” expression
  • The latter expression refers to the “false” expression.

Example
Let’s overview the below-given example:

<script type="text/javascript">
let value = 5;
let get = (value != 0) ?
 console.log('The value is not zero'):
 console.log('The value is zero');
</script>

Implement the following steps as given in the above demonstration:

  • Likewise, specify the stated value.
  • In the next step, apply the “ternary” operator alongside the comparison operator(!=) to check if the specified value in the previous step equals “0” or not.
  • Upon the satisfied condition, the former statement will be displayed, referring to the “ternary” operator’s syntax. The latter statement will be logged on the console in the other scenario.

Output

The above output signifies that the desired requirement is fulfilled.

Conclusion

The comparison operators in combination with the “if/else” condition or the “Ternary operator can be applied to check if a value is not equal to 0 using JavaScript. The former approach can be implemented to apply a condition upon the specified or the user-defined value for fulfilling the desired requirement. The latter approach can be utilized likewise to apply a condition such that upon the satisfied and unsatisfied conditions, the first and the second statements are displayed, respectively. This blog demonstrated to check if a value is not equal to 0 in JavaScript.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.