String comparison in JavaScript3 Mar 2025 | 4 min read String comparison is a foundational aspect of programming. In particular, web development frequently depends on user input, data validation, and conditional logic. It is where string comparison is often utilized. A trusted language, JavaScript has many methods to compare strings. This article delves into different techniques and best ways of practicing string comparison in JavaScript. These are complemented by well-documented code examples. 1. Basic String Comparison Using === and ==JavaScript uses the strict equality operator (===) to compare two strings for both value and type. It means that you'll consider them equal only if they have the same characters and length. Code Explanation: The loose equality operator (==) also compares two strings but performs type coercion if necessary. Thus, both operands coerce to string and comparison is the same as in the case of ===. It should call to mind that string comparisons necessarily differ in case. This means that "Hello" and "hello" are not equal. Output: true false true false 2. Case-Insensitive ComparisonIt is conventional to change both strings (either lower or upper) to one common case before the comparison, if the case should be ignored. Code Explanation: In this example, toLowerCase() is used for both stringA and stringB to convert them into lowercase format. This is so the case discrepancy doesn't count in the result. Output: true 3. Locale-Aware String Comparison with localeCompareThe localeCompare method gives a way for comparison of the strings by the way of the locales. It allows the string to be sorted in a way that the user will find most easily understandable. Code Explanation: The localeCompare method will yield a negative number, zero, or a positive number depending on whether the reference is less than, equal to, or greater than the comparison. Incidentally, besides offering case-insensitive options with sensitivity='base', the method also even allows for such comparisons. Output: -1 0 4. Checking Substring Presence with includes and indexOfSome situations arise wherein you want to check if a string actually implements a particular substring. JavaScript solves this problem by the includes() method that checks for the presence of the substring. Code Explanation: The includes() method in JavaScript is a built-in function that returns a boolean evaluating whether the string includes the substring. The difference is that indexOf, on the other hand, returns a positive value if the substring is found at the starting position, or if it returns -1 then it is not found. Output: true 16 5. Comparing Strings Using Regular ExpressionsRegular expressions make it possible to accomplish pattern matching and comparing text strings in a complex way. Code Explanation: In the example shown, the regular expression /rain/i is used for searching for the occurrence of the word "rain" regardless of the case. The test method tells if the pattern is in the source string and returns a true/false value. Output: True ConclusionComparing string variables in JavaScript is a common task for programmers coming up with different ideas, each one demanding to be compared according to different methods. Be it through direct comparison, case-mismatch, specific language region, or partial search, one will know string handling more precisely and therefore improve the validation capacity of the language. Next TopicJavaScript Square |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India