| by Arround The Web | No comments

How to Compare Two Strings in JavaScript

[There are some situations where the developers need to compare two strings. Most programmers frequently make the mistake of attempting to compare their strings in JavaScript using the == operator. The == operator has some restrictions in this situation as the variable types cannot be validated using it. So, you may need to look for other methods to handle the situation.

This article will demonstrate the procedure for comparing strings in JavaScript.

How to Compare Two Strings in JavaScript?

For comparing two JavaScript strings, use the following listed methods:

Let’s examine the working of each method separately.

Method 1: Compare Two Strings Using strict equality Operator

The strict equality operator (===) can be utilized to validate whether the strings are equal or not. If the strings are different, it returns false as an output; else, it gives true. As it is a case-sensitive method, while comparing strings, it also compares the case of the letters, which means that lowercase and uppercase letters are considered different.

Syntax

Use the below syntax for comparing two strings using the strict equality operator:

str1===str2;

Here, the “===” operator will compare the value and the datatype of str1 and str2.

Example

In this example, we will compare the two strings one is in upper case, and the other is in lowercase. To do so, first, we will create two variables named “str1” and “str2” that stores strings “LinuxHint” and “linuxhint”:

var str1 = "LinuxHint";
var str2 = "linuxhint";

Then, call the “console.log()” method for printing the result that tells whether the strings are equal or not using the “===” operator:

console.log(str1 === str2);

The output shows “false” which indicates that the strings are not equal because the equality operator performs case-sensitive comparison:

If you want to perform a case-insensitive comparison between strings, you have to follow the below-given methods.

Method 2: Compare Two Strings Using localeCompare() Method

localeCompare()” is a JavaScript predefined method used for comparing two strings. It performs a case-insensitive string comparison and utilizes the current locale to compare two strings.

Syntax

Follow the below-mentioned syntax for comparing two strings using the localeCompare() method:

str1.localeCompare(str2);

Here, “str2” is the string that will be compared with “str1”:

Example

We will now use the same strings that are already created in the previous example and then compare them using the “localeCompare()” method with the help of the conditional operator:

if(str1.localeCompare(str2)){
    console.log("str1 is equals to str2");
}else{
    console.log("str1 is not equals to str2");
}

As you can see that the output indicates the strings are equal because the method compares the case-insensitive comparison:

Let’s move to the next method!

Method 3: Compare Two Strings Using RegExp With test() Method

There is another method for comparing two strings that also performs case-insensitive comparison called “RegExp” with the help of the “test()” method. It checks the equality of strings based on the defined regex. The test() method will accept a string as an argument for comparison.

Syntax

Follow the given syntax for the test() method using RegEx with test() method:

regPattern.test(str2);

Here, “str2” is the string that will be compared with “regPattern”:

Example

Now, we will create an instance of RegExp by passing a string “str1” and a regex “gi” that indicates the case-insensitive comparison and stores it in a variable “regPattern”:

var regPattern = new RegExp(str1, "gi");

After that, we will call the “test()” method by passing the string “str2” as an argument.

var ans = regPattern.test(str2);

Then, use the conditional statement to verify whether the strings are equal or not:

if(ans) {
    console.log("str1 is equals to str2");
} else {
    console.log("str1 is not equals to str2");
}

Output

If you want to compare a string with another string as a substring, you must follow the below section.

Method 4: Compare Two Strings Using includes() Method

To determine whether the string is a substring of the other string, JavaScript provides a predefined method called the “includes()” method. It takes a string as an argument and gives a boolean value “true” if it exists in the string; else, it returns “false”.

Syntax

Follow the below-provided method for using the includes() method:

str1.includes(str2);

Here, “str2” is the string that checks whether it is included or a part of the “str1”:

Example

Here, first, we will create two strings, “str1” and “str2” and check whether the second string is a substring of first or not:

var str1 = "LinuxHint";
var str2 = "in";

For this, call the “includes()” method by passing the “str2” as an argument:

console.log(str1.includes(str2));

The output gives “true” which indicates that the “str2” is the part of the “str1”:

We have compiled all the methods for comparing two JavaScript strings.

Conclusion

For comparing two JavaScript strings, you can use the localeCompare() method, strict equality operator, RegEx with test() method, or includes() method. The strict equality operator performs a case-sensitive comparison while the test() method and the localeCompare() method perform the case-insensitive comparison. Lastly, the includes() method checks the string as a substring in another string. In this article, we demonstrated the methods for comparing two strings in JavaScript with proper examples.

Share Button

Source: linuxhint.com

Leave a Reply