JavaScript Program to Check if Two Strings are Same or Not

In this article, we are going to implement a JavaScript program to check whether two strings are the same or not. If they are the same then we will return true else we will return false.

Examples:

Input: str1 = Beginner, str2 = Beginner
Output: True. Strings are the Same
Input: str1 = Beginner, str2 = Geek
Output: False. Strings are not Same

Table of Content

  • Using localCompare() Method
  • Using startsWith() and endsWith() Methods
  • Using the String.prototype.match() Method
  • Using Array.from() and Array.prototype.join() Methods
  • Using Iterative Character Comparison

Using localCompare() Method

In this approach, we are using the localCompare() method in JavaScript. This method is mainly used to compare the two strings according to their locale-specific collation order. If the strings are the same, then 0 is returned; otherwise, -1 or 1 is returned.

Syntax:

str1.localeCompare(str2)

Example: This example shows the use of the above-explained approach.

Javascript
const sameStrings = (
    inputString1,
    inputString2) => {
    return (
        inputString1.localeCompare(
            inputString2 ) === 0
  );};
console.log(
    sameStrings("Beginner", "Beginner")
);

Output
true

Using startsWith() and endsWith() Methods

In this approach, we are using the startsWith() and endsWith() methods in JavaScript. Here, we are checking if str1 starts and ends with str2. If both considerations are satisfied, then the strings are the same when they are not the same.

Syntax:

 str1.startsWith(str2) && str1.endsWith(str2)

Example: This example shows the use of the above-explained approach.

Javascript
const stringsSame = (
    inputString1,
    inputString2) => {
    return (
        inputString1.startsWith(
            inputString2
        ) &&
        inputString2.endsWith(
            inputString1
        )
    );};
console.log(
    stringsSame("Beginner", "Beginner")
);

Output
true

Using the String.prototype.match() Method

In this approach, we are using the match() method along with the regular expression. Here we are checking if str1 is the same as str2 using the regular expression pattern. If they are the same, then the true message is printed; otherwise, the false method is printed.

Syntax:

str1.match(new RegExp(`^${str2}$`)) !== null 

Example: This example shows the use of the above-explained approach.

Javascript
const sameString = (
      inputString1,
      inputString2) => {
    return (
        inputString1.match(
            new RegExp(
                `^${inputString2}$`
            )
        ) !== null
);};
console.log(
    sameString("Beginner", "Beginner")
);

Output
true

Using Array.from() and Array.prototype.join() Methods

In this approach, we are using the from() and join() methods in JavaScript. Here, we are converting the input strings into an array of characters, then comparing these characters using the (===) operator, and then joining them back to strings. If the strings are the same, then the output is true; otherwise, it is false.

Syntax:

Array.from(str1).join('') === Array.from(str2).join('') 

Example: This example shows the use of the above-explained approach.

Javascript
const sameString = (
    inputString1,
    inputString2 ) => {
    return (
        Array.from(inputString1).join(
            "") ===
        Array.from(inputString2).join(
            "")
);};
console.log(
    sameString("Beginner", "Beginner")
);

Output
true

Using Iterative Character Comparison

Using iterative character comparison involves first checking if the lengths of the strings are equal. If they are, compare each character one by one using a loop. If all characters match, the strings are considered the same.

Example:

JavaScript
function areStringsSame(str1, str2) {
  if (str1.length !== str2.length) return false;
  for (let i = 0; i < str1.length; i++) {
    if (str1[i] !== str2[i]) return false;
  }
  return true;
}


console.log(areStringsSame("hello", "hello")); // Output: true
console.log(areStringsSame("hello", "world")); // Output: false

Output
true
false