To check if two strings are equal in JavaScript, you can use the strict equality operator ===
or the ==
operator.
In this example,
str1
and str2
with the same value "Hello"
.str1 === str2
, which will return true
if they are equal and have the same type, and false
otherwise.let str1 = "Hello";
let str2 = "Hello";
let result = str1 === str2;
console.log(`Are strings equal? ${result}`);
Are strings equal? true
In this example,
str1
and str2
with the same value "Hello"
.str1 == str2
, which will return true
if they are equal, even if they are of different types, and false
otherwise.let str1 = "Hello";
let str2 = "Hello";
let result = str1 == str2;
console.log(`Are strings equal? ${result}`);
Are strings equal? true
In this tutorial, we learned How to check if Two Strings are Equal in JavaScript language with well detailed examples.