JavaScript Code


How to check if two strings are equal in JavaScript?


Check if two strings are equal in JavaScript

In this tutorial, you are given two strings. You should check if the two strings are equal using JavaScript.

Solution

Two strings are equal if they hold the same string value. To compare two strings if they are equal using JavaScript, we can use comparison equal-value and equal-type operator. This operator returns true if the two strings are equal, or false otherwise.

Program

In the following program, we are given two strings in str1 and str2. We shall check if str1 and str2 are equal.

let str1 = 'apple';
let str2 = 'apple';
if ( str1 === str2 ) {
    console.log('two strings are equal');
} else {
    console.log('two strings are not equal');
}