Javascript not equal multiple values

Table of Contents #

  1. Check if variable is equal to ALL of multiple values
  2. Check if variable is equal to ONE of multiple values

Check if Variable is equal to Multiple Values #

To check if a variable is equal to all of multiple values:

  1. Place the values in an array and use the every[] method to iterate over the array.
  2. Check if each value is equal to the variable.
  3. The every method will return true if the variable is equal to all the values.

Copied!

const str = 'hello world'; const val1 = 'hello world'; const val2 = 'hello world'; const val3 = 'hello world'; const result = [val1, val2, val3].every[value => { return value === str; }]; console.log[result]; // 👉️ true

The function we passed to the Array.every method gets called with each element in the array until it returns a falsy value or iterates over the entire array.

We compare each value against the variable and return the result.

If the comparison fails, the every method short-circuits and returns false, otherwise it returns true.

An alternative approach is to use the logical AND [&&] operator.

To check if a variable is equal to all of multiple values, use the logical AND [&&] operator to chain multiple equality comparisons. If all comparisons return true, all values are equal to the variable.

Copied!

const str = 'hello world'; const val1 = 'hello world'; const val2 = 'hello world'; const val3 = 'hello world'; if [str === val1 && str === val2 && str === val3] { console.log['✅ variable is equal to all of the values']; } else { console.log['⛔️ variable is not equal to all of the values']; }

We used the logical AND [&&] operator to chain multiple equality checks.

The logical AND [&&] operator returns the value to the left if it's falsy, otherwise it returns the value to the right.

All the equality checks have to return true for the if block to run.

Check if Variable is equal to One of Multiple Values #

To check if a variable is equal to one of multiple values:

  1. Add the values to an array.
  2. Call the includes[] method on the array.
  3. The includes method will return true if the value is contained in the array.

Copied!

const str = 'hello world'; const val1 = 'hello world'; const val2 = 'bye world'; const val3 = 'test world'; if [[val1, val2, val3].includes[str]] { console.log['✅ variable is equal to at least 1 of the values']; } else { console.log['⛔️ variable is not equal to any of the values']; }

We used the Array.includes method to check if a value is contained in an array of values.

The method returns true if the value is contained in the array and false otherwise.

You can also use the indexOf method instead of includes.

Copied!

const str = 'hello world'; const val1 = 'hello world'; const val2 = 'bye world'; const val3 = 'test world'; if [[val1, val2, val3].indexOf[str] !== -1] { console.log['✅ variable is equal to at least 1 of the values']; } else { console.log['⛔️ variable is not equal to any of the values']; }

We used the Array.indexOf method to check if a value is contained in an array of values.

The method returns the index of the first occurrence of the value in the array or -1 if the value is not contained in the array.

The if statement checks if the return value of the indexOf method is not equal to -1. If the condition is met, the variable is equal to at least 1 of the values.

Further Reading #

  • Check if Multiple Values exist in Array in JavaScript
  • Check if String starts with one of Multiple Values in JS

Does != Work in JavaScript?

” in JS? The JavaScript not equal or inequality operator [!=] checks whether two values are not equal and returns a boolean value. This operator tries to compare values irrespective of whether they are of different types.

How do you write not equal in JavaScript condition?

The strict inequality operator [ !== ] checks whether its two operands are not equal, returning a Boolean result. Unlike the inequality operator, the strict inequality operator always considers operands of different types to be different.

What is === in JavaScript example?

The strict equality operator [ === ] checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

How do you know if a variable equals multiple values?

To check if a variable is equal to all of multiple values, use the logical AND [&&] operator to chain multiple equality comparisons. If all comparisons return true , all values are equal to the variable. Copied! We used the logical AND [&&] operator to chain multiple equality checks.

Chủ Đề