JavaScript

Is != the Same as !== Operators in JavaScript

While programming in JavaScript, there can be situations where there is a need to compare two values simply or consider their data types as well. In such a scenario, the “inequality operator(!=)” and the “strict inequality operator(!==)” come in handy to analyse the data in bulk, thereby saving time on the developer’s end.

This tutorial will illustrate the difference between JavaScript “(!=)” and “(!==)” operators.

In JavaScript, is the Inequality Operator(!=) the Same as the Strict Inequality (!==) Operator?

Both the “inequality(!=)” and “strict equality(!==)” operators return the result in boolean form. The inequality(!=) operator is utilized to differentiate between the operands regardless of their data types. Whereas the strict inequality operator(!==) checks if the operands and their data types both are unequal.

Example 1: Utilization of the Inequality Operator(!=) and the Strict Inequality(!==) Operator Upon the Specified Value

In this example, the “inequality operator(!=)” and “strict inequality(!==)” operators will be utilized to apply a check upon the specified value:

<script>

let a='4';

if (a!=4) {

console.log('a is not equal to 4');

}

else{

console.log('a is equal to 4');

}

if (a!==4) {

console.log('a is not strictly equal to 4');

}

else{

console.log('a is equal to 4');

}

</script>

In the above code snippet:

  • Firstly, create the variable named “a” and assign a string value that is a valid number.
  • In the next step, apply the “if/else” condition with the “inequality operator(!=)” to compare the defined string value with an integer.
  • In this case, since the values are the same irrespective of the data type, hence the “else” condition will execute.
  • Likewise, apply the “if/else” condition with the “strict inequality(!==)” operator.
  • Here, the “strict inequality(!==)” operator checks the values and their data types. Therefore the “if” condition will execute.

Output

In the above output, it can be seen the difference between the applied operands can be observed.

Example 2: Utilization of the Inequality Operator(!=) and the Strict Inequality(!==) Operator Upon the User-defined Value

In this particular example, the “inequality(!=)” and “strict inequality(!==)” operators will be applied upon the user-defined value:

<script>

let x = 5;

let a = prompt('Enter the value :')

if (a!=x) {

console.log(a + ' is not equal to 5');

}

else{

console.log(a + ' is equal to 5');

}

if (a!==x) {

console.log(a + ' is strictly not equal to 5');

}

else{

console.log(a + ' is equal to 5');

}

</script>

In the above code block:

  • Likewise, create a variable named “x” and assign an integer value.
  • After that, input a value from the user to compare it with the initialized value.
  • Now, use the “inequality(!=)” operator in the “if/else” condition.
  • In this scenario, similarly, the “else” condition will be executed.
  • Note that the “prompt” dialogue box returns a “null” or “string” value.
  • In the next line, apply the “if/else” condition along with the “strict inequality(!==) ” operator.
  • In this case, the “if” statement will be invoked since the prompt returns the “string” data type, not identical to the datatype of the initialized value.

Output

That was all about comparing values using inequality and strict inequality operator.

Conclusion

The “inequality(!=)” operator compares the operands only, whereas the “strict inequality(!==)” operator compares both the operands and their data types. This write-up stated the core differences between the (!=) and (!==) operators in JavaScript with the help of examples.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.