JavaScript Program to Compare Two Strings Lexicographically Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 string comes after the second string, a negative value if the first string comes before the second string, and zero if the strings are equal. Table of Content Using LocaleCompare MethodUsing Custom ComparisonUsing LocaleCompare MethodJavaScript's localeCompare() method compares two strings lexicographically and returns a value indicating their relative ordering. Example: The below code example Uses the LocaleCompare Method to Compare Two Strings Lexicographically in JavaScript. JavaScript function compareStrings(str1, str2) { return str1.localeCompare(str2); } // Example usage: console.log(compareStrings("apple", "banana")); console.log(compareStrings("apple", "apple")); Output-1 0 Using Custom ComparisonIn this approach, we manually compare the characters of the strings one by one to determine their lexicographical order. Example: The below code example Uses the Custom Comparison Method to Compare Two Strings Lexicographically in JavaScript. JavaScript function compareStrings(str1, str2) { for (let i = 0; i < Math.min(str1.length, str2.length); i++) { if (str1[i] !== str2[i]) { return str1[i].charCodeAt(0) - str2[i].charCodeAt(0); } } return str1.length - str2.length; } // Example usage: console.log(compareStrings("apple", "banana")); console.log(compareStrings("apple", "apple")); Output-1 0 Comment More infoAdvertise with us Next Article JavaScript Program to Compare Two Strings Lexicographically A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads 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 C Program to Compare Two Strings Lexicographically In C, lexicographic comparison refers to comparing each character of one string to the character of other string at same position based on their alphabetical order, just like how words are arranged in a dictionary. In this article, we will learn how to compare two strings lexicographically using the 3 min read Program to Compare two Strings in R String comparison is a common operation where two strings are compared in a case sensitive manner and check whether they are equal or not. In this article you will learn the various way in which you can compare two string in R language. Concepts Related to the Topic :String Comparison: Comparison of 4 min read C Program to Compare Two Strings Using Pointers In C, two strings are generally compared character by character in lexicographical order (alphabetical order). In this article, we will learn how to compare two strings using pointers.To compare two strings using pointers, increment the pointers to traverse through each character of the strings whil 2 min read PHP Program to Check if Two Strings are Same or Not Given two strings, the task is to check whether two strings are same or not in PHP. In this article, we will be looking at different approaches to check two strings are same or not. Examples: Input: str1 = "Geeks", str2 = "Geeks" Output: Both Strings are the same Input: str1 = "Hello", str2 = "Geeks 2 min read Like