Example 1: Using toUpperCase()
Output
The strings are similar.
In the above program, two strings are compared. Here,
- The
toUpperCase()method converts all the string characters to uppercase. ===is used to check if both the strings are the same.- The
if...elsestatement is used to display the result as per the condition.
Note: You can also use the toLowerCase() method to convert all the strings to lowercase and perform the comparison.
Example 2: JS String Comparison Using RegEx
Output
The strings are similar.
In the above program, the RegEx is used with the test() method to perform case insensitive string comparison.
In the RegEx pattern, "g" syntax denotes global and "gi" syntax denotes case insensitive comparisons.
Example 3: Using localeCompare()
Output
The strings are similar.
In the above program, the localeCompare() method is used to perform case insensitive string comparison.
The localeCompare() method returns a number that indicates whether a reference string comes before, or after, or is the same as the given string.
Here, { sensitivity: 'base' } treats A and a as the same.