Compare Two Strings in JavaScript | String Comparison


This article is on how to compare two strings in JavaScript using various methods with examples.

We can check if two string values are same or not using the strict equality operator === , if it returns false, that means the strings are not equal and true, if they are equal.

Lets check the different methods of string comparison along with examples.

Method 1 : Compare case-sensitive strings in JavaScript

To compare two case-sensitive strings in JavaScript, first we have to convert it to same case using toLowerCase() and toUpperCase() method.

First lets see an example with case-sensitive strings.

const str1 = 'hello'
const str2 = 'Hello'

console.log(str1 === str2) // false

As you can see when compared using strict equality === we get the output as false. This is because === checks the case of the string, i.e capital letters are different then small letters.

So to solve this, first we have to convert both string into same case (uppercase or lowercase) and then compare both the strings.

Example:

const str1 = 'hello'
const str2 = 'Hello'

console.log(str1.toLowerCase() === str2.toLowerCase()) // true

We have used toLowerCase() to convert both the strings to lowercase.

Method 2 : Using localeCompare() for string comparison in JavaScript.

The localeCompare() method is use to compare two string values in current locale i.e the region setting. The current locale is based on the user's settings.

Syntax:

String.localeCompare(compareString, locales, options)

compareString : string against which the reference string is compared.

locales: It lets to set the language that the string would use to compared. Example 'en', 'fr', 'de'.

options : It lets us to specify the behaviour of the method like case sensitivity. The sensitivity have four options:

  • base (a ≠ b, a = á, a = A)
  • accent (a ≠ b, a ≠ á, a = A)
  • case (a ≠ b, a = á, a ≠ A)
  • variant (default) (a ≠ b, a ≠ á, a ≠ A)

If localeCompare() returns a positive or negative number, it means the strings are not equal. If the output is zero (0), it means the strings are equal.

Lets see an example:

const string1 = 'hello'
const string2 = 'Hello'

function compareString(str1, str2){
    const output = str1.localeCompare(str2, undefined, { sensitivity : 'base' })
    if(output == 0){
      return 'Strings are equal'
    } else {
      return 'Strings are not equal'
    }
}

console.log(compareString(string1, string2))

Output:

Strings are equal

Code Explanation:

We have a function compareString() which takes in two strings to compare.

We have set locales to undefined. It will use the user settings.

{ sensitivity : 'base' } we have set sensitivity to base to do case-insensitive comparison. It will treat A and a as same.

If the result is 0 , the function will return "Strings are equal" , else it will return "Strings are not equal".

Related Topics:

How Can I Do String Interpolation In JavaScript?

Truncate A String In JavaScript With Example

How To Remove Character From A String

JavaScript - String Replace All Appearances

Check If String Is A Number Or Not In JavaScript

Check If String Is Empty In JavaScript

Convert String To Float In JavaScript

Check If A String Contains A Substring In Javascript


💾 Recommended Articles