Optimum way to compare strings in JavaScript
Last Updated :
07 Jun, 2023
In this article, we will know the optimal way to compare the strings using built-in JavaScript methods & will see their implementation through the examples. The question is to compare 2 JavaScript strings optimally. To do so, here are a few of the most used techniques discussed. The method discussed below is used in the following examples.
String localeCompare() Method: This method compares two strings in the current locale. The current locale is based on the language settings of the browser. This method returns a number that tells whether the string comes before, after, or is equal to the compareString in sort order.
Syntax:
string.localeCompare(String_2);
Parameters:
- String_2: This required parameter specifies the string to be compared with.
Please refer to the JavaScript Operators Complete Reference article for further details on operators.
Example 1: This example compares the 2 strings using localeCompare() method and returns 0, -1, or 1. This method does case-sensitive comparisons.
JavaScript
let str1 = "GFG"
let str2 = "GeeksforGeeks"
let ans = str1.localeCompare(str2);
let res = "";
if (ans == -1) {
res = '"' + str1 + '" comes before "' + str2 + '"';
} else if (ans === 0) {
res = 'Both string are same';
} else {
res = '"' + str1 + '" comes after "' + str2 + '"';
}
console.log(res);
Output"GFG" comes after "GeeksforGeeks"
Example 2: This example compares the 2 strings by writing a condition that returns 0, -1, or 1 depending on the comparison. This method also does case-sensitive comparisons.
JavaScript
let str1 = "Geeks"
let str2 = "GFG"
let ans = str1 < str2 ? -1 : (str1 > str2 ? 1 : 0);
let res = "";
if (ans == -1) {
res = '"' + str1 + '" comes before "' + str2 + '"';
} else if (ans === 0) {
res = 'Both string are same';
} else {
res = '"' + str1 + '" comes after "' + str2 + '"';
}
console.log(res);
Output"Geeks" comes after "GFG"
Example 3: This example compares the 2 same strings (case-sensitive also) by using the localeCompare() method.
JavaScript
let str1 = "GeeksforGeeks"
let str2 = "GeeksforGeeks"
let ans = str1.localeCompare(str2);
let res = "";
if (ans == -1) {
res = '"' + str1 + '" comes before "' + str2 + '"';
} else if (ans == 0) {
res = 'Both string are same';
} else {
res = '"' + str1 + '" comes after "' + str2 + '"';
}
console.log(res);
OutputBoth string are same
Similar Reads
How to Compare Objects in JavaScript? Comparing objects is not as simple as comparing numbers or strings. Objects are compared based on their memory references, so even if two objects have the same properties and values, they are considered distinct if they are stored in different memory locations. Below are the various approaches to co
3 min read
Sort a String in JavaScript Here are the most used methods to sort characters in a string using JavaScript.Using split(), sort(), and join()The most simple way to sort a string is to first convert it to an array of characters, sort the array, and then join it back into a string.JavaScriptlet s1 = "javascript"; let s2 = s1.spli
2 min read
JavaScript - Compare the Case Insensitive Strings Here are the different methods to compare the case-insensitive strings in JavaScript.1. Using toLowerCase() or toUpperCase() MethodsThis is the most simple and used approach. Convert both strings to the same case and then compare them.JavaScriptconst comStr = (s1, s2) => s1.toLowerCase() === s2.t
2 min read
JavaScript â===â vs â==âComparison Operator In Javascript(ES6), there are four ways to test equality which are listed below: Using '==' operatorUsing '===' operatorSameValueZero: used mainly in sets, maps and arrays.SameValue: used elsewhereNow, our main concern is getting to know the difference between the '==' and '===' operators that the j
3 min read
How are strings stored in JavaScript ? In this article, we will try to understand how strings are stored in JavaScript. Strings are a primitive data type in JavaScript and are allocated a special place in memory to store and manipulate. Unlike some other languages, JavaScript does not have a String Constant Pool. Instead, JavaScript engi
3 min read