nodejs string value check === and !==
Node.js String Value Check with === and !==
In Node.js, you can use the ===
and !==
operators to check the equality and inequality of string values. These operators are strict equality operators, meaning they not only compare the values of the strings but also their types.
The ===
operator checks if two string values are exactly equal, both in value and type. It returns true
if the values and types are the same, and false
otherwise.
The !==
operator checks if two string values are not equal, either in value or type. It returns true
if the values or types are different, and false
if they are the same.
Here's an example to illustrate the usage of ===
and !==
operators for string value checks in Node.js:
let str1 = 'hello';
let str2 = 'world';
console.log(str1 === str2); // false
console.log(str1 !== str2); // true
let str3 = 'hello';
let str4 = 'hello';
console.log(str3 === str4); // true
console.log(str3 !== str4); // false
In the example above, the ===
operator compares the values and types of the string variables str1
and str2
. Since they have different values, the result is false
. On the other hand, the !==
operator returns true
because the values are different.
Similarly, when comparing str3
and str4
, both the ===
and !==
operators return true
and false
, respectively, because the values and types are the same.
Please note that the ===
and !==
operators can also be used to compare other types of values, not just strings.