JavaScript - Compare Two Strings Last Updated : 25 Nov, 2024 Comments Improve Suggest changes Like Article Like Report These are the followings ways to compare two strings: 1. Using strict equality (===) operatorWe have defined a function that compare two strings using the strict equality operator (===), which checks if both the value and the type of the operands are equal. JavaScript let str1 = "hello"; let str2 = "world"; if (str1 === str2) { console.log("Equal"); } else if (str1 > str2) { console.log("str1 is greater"); } else if (str1 < str2) { console.log("str2 is greater"); } Outputstr2 is greater 2. Using localeCompare( ) methodThe localeCompare( ) method, which compares two strings based on the locale-sensitive comparison rules and returns: A negative number if the first string comes before the second string. Zero if the strings are equal. A positive number if the first string comes after the second string. JavaScript let str1 = "apple"; let str2 = "banana"; let res = str1.localeCompare(str2); if (res === 0) { console.log("Strings are equal"); } else if (res < 0) { console.log("str2 is greater"); } else if (res > 0) { console.log("str1 is greater"); } Outputstr2 is greater 3. Using Regular ExpressionRegular expressions compares two strings based on specific patterns or criteria. We can use Regular Expression to verify if two given strings are same or not. However, this method is not case sensitive. JavaScript let str1 = "Hello"; let str2 = "hello"; let regex = new RegExp(`^${str2}$`, 'i'); if (regex.test(str1)) { console.log("Equal"); } else if (str1 > str2) { console.log("str1 is greater"); } else { console.log("str2 is greater"); } OutputEqual Comment More infoAdvertise with us Next Article JavaScript - Compare Two Strings A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-Program Similar Reads JavaScript Program to Compare Two Strings Lexicographically This JavaScript program compares two strings lexicographically, meaning it checks if one string comes before or after the other in alphabetical order. Lexicographical comparison is based on the Unicode value of each character in the strings. The program should return a positive value if the first st 2 min read 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 = Geeks, str2 = GeeksOutput: True. Strings are the SameInput: str1 = Geeks, str2 = GeekOutpu 4 min read Optimum way to compare strings in JavaScript 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 discu 3 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 How to Compare Strings in C#? A string is a collection of characters and is used to store plain text. Unlike C or C++, a string in C# is not terminated with a null character. The maximum size of a string object depends on the internal architecture of the system. A variable declared followed by "string" is actually an object of s 13 min read Like