We are going to learn two ways to check if variable doesn’t equal multiple values in JavaScript. Let’s read this article now.
Check If Variable Doesn’t Equal Multiple Values In JavaScript
Using if statement and && operators
The if statement is well-known by most programmers in terms of comparing variables’ values. And the logical and operator is commonly used inside the if statement whenever they have to check many conditions simultaneously.
You can pass a condition to the if statement to compare equality using a strict inequality (!==) operator or a normal inequality (!=) operator.
let variable = 'LearnShareIT'
// check if variable doesn’t equal to ‘LEARN SHARE IT’ and ‘LEARNSHAREIT’ using !==
if (variable !== 'LEARN SHARE IT' && variable !== 'LEARNSHAREIT')
console.log(variable + " is not equal to both values checked")
Output:
LearnShareIT is not equal to both values checked
The above example uses the strict inequality operator to compare the variable’s value. However, the normal inequality operator also work in this case:
let variable = 'LearnShareIT'
// check if variable doesn’t equal to ‘LEARN SHARE IT’ and ‘LEARNSHAREIT’ using !=
if (variable != 'LEARN SHARE IT' && variable != 'LEARNSHAREIT')
console.log(variable + " is not equal to both values checked")
Output:
LearnShareIT is not equal to both values checked
So what is the difference? The strict inequality will ensure the variable type is also the same as the values you have passed to check. This means if you provide a number and want to check if they are not equal to the values, such as strings, it will return unequal results immediately. Because, at first, they are not 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 following example:
let variable = 1
// check if the variable isn't equal to ‘1’ and ‘0’ using !=
if (variable != '0' && variable != '1')
console.log(variable + " is not equal to both values checked")
else console.log(variable + " is equal to a value checked")
Output:
1 is equal to a value checked
Now we have seen the difference between each operator. To decide using which operator depends on what you actually expect to receive.
Using if statement in a for loop
The first solution will help you check the variable with a small number of values. When it comes to a larger number of values you want to compare, you should think about a loop instead because it will help you shorten your if statement’s code:
// define the array of values you want to compare it with the variable
let array = ['learn',undefined,'share',null,'it'];
// define the variable you want to compare with multiple values
let variable = 'LearnShareIT'
// iterate through all elements and check if the variable is equal to any values in the given array or not
for (i = 0 ; i < array.length; i++)
if (array[i] === variable )
console.log(variable + ' is equal to '+array[i])
else console.log(variable + ' is not equal to '+array[i])
Output:
LearnShareIT is not equal to learn
LearnShareIT is not equal to undefined
LearnShareIT is not equal to share
LearnShareIT is not equal to null
LearnShareIT is not equal to it
This solution is simpler than the previous one. However, this approach also gives out the same result because we have used a strict equal operator (===) to ensure it is equal in both values and types. Therefore, we successfully checked whether variable doesn’t equal multiple values or not.
Summary
We have learned several ways to check if variable doesn’t equal multiple values in JavaScript. Please consider that each operator (strict or normal equality/inequality) has different advantages, so you should choose the best one for your desired result.
Maybe you are interested:
- Check if variable is Null or Undefined in JavaScript
- Check if Variable is of Function Type using JavaScript
- Check if a Variable is of type Error 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