Site icon codippa

How to compare strings in javascript / 3 ways of string comparison in javascript

String comparison is a necessary operation required in every application and is used to determine if two string values are equal to one another or not.
Javascript provides provisions to compare two strings with each other to check if they are equal or not. This article will list down different ways in which two strings can be compared in javascript.
Method 1: Using equality operator
Javascript’s equality operator can be used to compare 2 strings. Equality operator consists of double equal to sign(==) with strings to be compared on its either sides.
It will return true if the strings are equal or same and false if the strings are not equal.
Note that == does a case sensitive comparison, that is, strings "codippa" == "Codippa" will be false.
Example,

let stringOne = "javascript";
let stringTwo = "javascript";
console.log(stringOne == stringTwo); // prints true

Method 2: Using Relational operators
Relational operators greater than(>)  and lesser than(<) can be used to compare two strings in javascript.
When using lesser than(<) operator, if the characters of the string to the left of operator fall alphabetically earlier than the characters of the string to the right side, the result will be true, false otherwise.
Similarly, when using greater than(>) operator, if the characters of the string to the left of operator fall alphabetically later than the characters of the string to the right side, the result will be true, false otherwise.
Example,

let lesserThan = "tear" < "bear";
console.log(lesserThan);  // prints false
let lesserThanAgain = "sink" < "wink";
console.log(lesserThanAgain);  // prints true
let greaterThan = "scala" > "react";
console.log(greaterThan);  // prints true
let greaterThanAgain = "sink" > "wink";
console.log(greaterThanAgain);  // prints true

Remember that the comparison is done starting from leftmost characters of both the strings.
Thus, with lesser than operator, if the first character of the left string is alphabetically earlier than the corresponding character of the right string, the result will be true.
Similarly, with greater than operator, if the first character of the left string falls after than the corresponding character of the right string alphabetically, the result will be true.
In case the first characters of both strings are same, then second character will be compared for result and so on.
Method 3: Using localeCompare method
localeCompare method is called on a string object and accepts another string as argument and returns the below values:

Example,

let greaterThan = "tear".localeCompare("bear");
console.log(greaterThan);  // prints 1
let lesserThan = "sink".localeCompare("wink");
console.log(lesserThan);  // prints -1
let equalTo = "javascript".localeCompare("javascript");
console.log(equalTo);  // prints 0

Remember that localeCompare does a case sensitive comparison. Thus, "tear".localeCompare("Tear") will return -1 and not 0 since 't' comes before 'T' alphabetically.
If you want a case insensitive comparison, then convert both the strings to lower case using toLowerCase or toLocaleLowerCase methods or convert both strings to upper case using toUpperCase or toLocaleUpperCase methods.
Example,

"tear".localeCompare("Tear".toLowerCase()); // returns 0
"tear".toUpperCase.localeCompare("Tear"); // returns 0

Hope you liked this post, click the clap to appreciate it.

Exit mobile version