Compare Strings in JavaScript

24/09/2023

To compare if two string values are equal to each other, use the code below. If the strings are equal, the result will be “true,” but if they are not equal, the result will be “false.”

Equal Strings

const string1 = "Hello";
const string2 = "Hello";

console.log(string1 === string2)

Result

Not Equal Strings

const string1 = "Hello";
const string2 = "World";

console.log(string1 === string2)

Result

Another Way to Check If Two Strings Equal

const string1 = "Hello";
const string2 = "World";

if (string1 === string2) {
  console.log("The strings are equal.");
} else {
  console.log("The strings are not equal.");
}