compare string in javascript

To compare two strings in JavaScript, you can use the === operator. The === operator checks for both the value and the type of the operands. Here's an example:

index.tsx
const string1 = "hello";
const string2 = "world";
const string3 = "hello";

console.log(string1 === string2); // false
console.log(string1 === string3); // true
161 chars
7 lines

In the example above, string1 is compared with string2 using the === operator, which returns false because they have different values. string1 is also compared with string3 using the === operator, which returns true because they have the same value.

Note that if you use the == operator instead of ===, JavaScript will try to convert the operands to a common type before comparing them. However, this can lead to unexpected results, so it's generally recommended to use === for string comparison.

related categories

gistlibby LogSnag