Open In App

JavaScript Program to Compare Two Strings

Last Updated : 01 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, comparing two strings can be essential for performing tasks like user authentication. It can involve checking if the user has entered the same credentials as there are in the database. This article provides insights into comparing two strings in JavaScript. It explores four distinct approaches with detailed explanations and examples.

Using strict equality (===) operator

In this approach we define a function that compare two strings using the strict equality operator (===), which checks if both the value and the type of the operands are equal.

Example: The below code example uses the strict equality (===) operator to Compare two Strings in JavaScript.

Javascript




function compareStrings(str1, str2) {
    if (str1 === str2) {
        return "Strings are equal";
    } else {
        return "Strings are not equal";
    }
}
 
console.log(compareStrings("hello", "hello"));


Output

Strings are equal

Using localeCompare( ) method

The localeCompare( ) method, which compares two strings based on the locale-sensitive comparison rules and returns:

  • A negative number if the first string comes before the second string.
  • Zero if the strings are equal.
  • A positive number if the first string comes after the second string.

Example: The below code example Uses the localeCompare( ) Method to Compare Two Strings in JavaScript.

Javascript




function compareStrings(str1, str2) {
    let comparisonResult = str1.localeCompare(str2);
    if (comparisonResult === 0) {
        return "Strings are equal";
    } else if (comparisonResult < 0) {
        return `"${str1}" comes before "${str2}"`;
    } else {
        return `"${str1}" comes after "${str2}"`;
    }
}
 
console.log(compareStrings("apple", "banana"));


Output

"apple" comes before "banana"

Using includes() method

The includes() method check if one string includes the other string. It returns true if one string includes the other one, otherwise it return false.

Example: The below code example uses the includes() method to Compare two Strings in JavaScript.

Javascript




function compareStrings(str1, str2) {
    if (str1.includes(str2)) {
        return `${str1} contains ${str2}`;
    } else {
        return `${str1} does not contain ${str2}`;
    }
}
 
console.log(compareStrings("hello world", "world"))


Output

hello world contains world

Using Regular Expression

Regular expressions compares two strings based on specific patterns or criteria. We can use Regular Expression to verify if two given strings are same or not. However, this method is not case sensitive.

Example: The below code example Uses the regular expressions Method to Compare Two Strings in JavaScript.

Javascript




function compareStrings(str1, str2) {
    // Case insensitive regular expression
    let regex = new RegExp(str2, 'i');
    if (regex.test(str1)) {
        return `${str1} matches the pattern ${str2}`;
    } else {
        return `${str1} does not match the pattern ${str2}`;
    }
}
 
console.log(compareStrings("Hello", "hello"));


Output

Hello matches the pattern hello



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads