OneBite.Dev - Coding blog in a bite size

compare two strings in Javascript

Code snippet on how to compare two strings in Javascript

var str1 = "Hello";
var str2 = "World";

if (str1 === str2) {
  console.log("Both strings are equal");
} else {
  console.log("Both strings are not equal");
}

This code is an example of how to compare two strings in Javascript. The first two lines create two string variables, str1 and str2. The first variable, str1, is equal to “Hello”, and the second variable, str2, is equal to “World”.

The third line is an if statement, which checks if str1 is the same as str2. If they are the same, it will run the code inside the curly braces (the fourth line). This line will print out “Both strings are equal” in the console. If they are not the same, it will run the code inside the else statement (the fifth line). This line will print out “Both strings are not equal” in the console.

javascript