 # How to check if a value is NOT equal to 0 using JavaScript Basically, there exists some built-in functions that can check if a value is NOT equal to 0 using JavaScript. We will learn to achieve it using a couple of different ways which are Math.sign() and inequality operator. Each way is relatively straightforward, so you can choose whichever fits you most.

## Check if a value is NOT equal to 0 using JavaScript

### Using Math.sign()

Syntax:

Math.sign(value)

Parameter:

• value: The value to check can be a string or a number.

The Math.sign() function returns 0 if the value is 0. Otherwise, it returns 1 when the value is a positive number, and -1 when the value is negative. For instance:

// Get the sign of -1234
let num = "-1234";
console.log(Math.sign(num));

// Get the sign of 0
num = "0";
console.log(Math.sign(num));

Output:

-1
0

As can be seen in the above example. We didn’t pass a number but instead a string that represents the number. However, the method still works fine as it has converted them into numbers for you when returning the result. As there is a zero number in our example, the method will return 0. Therefore, we can check if a value is not equal to 0 by checking if the function Math.sign() returns 0 or not:

// Check if a value is NOT equal to 0
let num = "-2022";

if (Math.sign(num)) {
console.log(num + " is not equal to  0");
} else {
console.log(num + " is equal to 0");
}

Output:

-2022 is not not equal to 0

Another example:

// Check if a value is NOT equal to 0
let num = "0";

if (Math.sign(num)) {
console.log(num + " is not equal to 0");
} else {
console.log(num + " is equal to 0");
}

Output:

0 is equal to 0

However, in some cases, if you want the string “0” is not equal to 0 but only the number 0 or 0.000 equals to 0 then you pass the number to the if statement and neglect the sign() method:

// Check if a value is NOT equal to 0
let num = "0";

if (num) {
console.log(num + " is not equal to 0");
} else {
console.log(num + " is equal to 0");
}

Output:

0 is not equal to 0

The logic behind this approach is very simple. As you want only the 0 number is equal to 0 but not strings such as “0” or “0.000”, you should get rid of the Math.sign() method and pass only the variable into its condition. The if statement works like this: if that value is equal to 0 or false, it executes the else statement instead. As we want to check if a value is not equal to 0, then the else statement is what we are expecting for. Whenever the else statement runs, it means that our value is equal to 0. Otherwise, the else statement will not run and our value is not equal to 0.

### Using inequality operator

Syntax:

value != 0

value !== 0

Parameter:

• value: The value to be checked (a number or numeric string).

We have introduced two different operators: the first one is the normal inequality operator and the second is the strict inequality operator. The second operator also checks if the value has a type of number instead of string or anything else. For example:

let num = "0";

// Check if string "0" is not equal to 0
if (num != 0) {
console.log(num + " is not equal to 0");
} else {
console.log(num + " is equal to 0");
}

Output:

0 is equal to 0

Another example:

let num = "0";

// Check if string "0" is not equal to 0
if (num !== 0) {
console.log(num + " is not equal to 0");
} else {
console.log(num + " is equal to 0");
}

Output:

0 is not equal to 0

As you can see, each operator will produce a different result when it comes to a numeric string such as “0” in the example above. It is the fact that most programmers are likely to use the strict inequality operator more than other ways. Which method is better to use depends on your decision and expectation.

## Summary

We have learned to check if a value is NOT equal to 0 using JavaScript with two different solutions. We recommend you use the second solution (strict inequality operator) as it is familiar with many programmers. Have a nice day!

Maybe you are interested: