Skip to content

JavaScript not equal string | Example code

  • by

Use the strict inequality (! ==) operator to check if two strings are not equal or not in JavaScript. This operator returns true if the strings are not equal and false otherwise.

You can find === and !== operators in several other dynamically-typed languages as well.

JavaScript not equal string

Simple example code check if two strings are not equal to one another.

<!DOCTYPE html>
<html lang="en">
<body>
  <script>
    const a = 'Hello';
    const b = 'Bye';

    if (a !== b) {
      console.log('✅ strings are NOT equal');
    } else {
      console.log('⛔️ strings are equal');
    }

  </script>
</body>
</html>

Output:

JavaScript not equal string

Remove all whitespace characters before comparing them? You can do it with a regexp like str.replace(/\s+/g, "").

More Examples

<script>
    console.log(5 !== '5'); // true
    console.log('one' !== 'one'); // false
    console.log('one' !== 'ONE'); // true
    console.log('' !== ' '); // true

    console.log(null !== null); // false
    console.log(undefined !== undefined); // false
    console.log(NaN !== NaN); // true
</script>

In JavaScript is != same as !==

They are subtly not the same.

!= checks the value
!== checks the value and type

'1' != 1   // false (these two are the same)
'1' !== 1 // true (these two are **not** the same).

Do comment if you have any doubts or suggestions on this Js equal topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *