Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to compare two strings in JavaScript

Unraveling the Mystery: Comparing Strings in JavaScript

Welcome to another exciting adventure in your journey to becoming a proficient programmer. Today, we are going to delve into a fundamental yet crucial aspect of JavaScript: comparing strings. If you've ever wondered how your computer manages to tell whether "apple" is the same as "apple" or if "Apple" should come before "apple" in a list, then you're in for a treat!

The Basics: JavaScript String Comparison

Before we jump into the deep end, let's make sure we're all on the same page. When we talk about comparing strings in JavaScript, we essentially mean checking if two strings are equal, or determining their order in relation to each other. It's the same as comparing numbers, but with text instead.

Here's an example:

let str1 = "Hello, World!";
let str2 = "Hello, World!";

console.log(str1 == str2); // Output: true

In this example, the two strings are exactly the same, so the comparison returns true.

Case Matters: Uppercase and Lowercase

Now, what if we tried to compare "Hello, World!" and "hello, world!"? JavaScript, like most programming languages, is case-sensitive. This means that it considers uppercase and lowercase letters as different characters. So, when we compare these two strings:

let str1 = "Hello, World!";
let str2 = "hello, world!";

console.log(str1 == str2); // Output: false

The result is false because, in the eyes of JavaScript, these two strings are not the same.

Behind the Scenes: ASCII Values

But how does JavaScript decide the order of different strings? It uses something called ASCII values. ASCII (American Standard Code for Information Interchange) is a character encoding standard used to represent text in computers. Each character, including letters, numbers, and symbols, has a corresponding ASCII value.

In ASCII, the uppercase 'A' has a value of 65, while the lowercase 'a' has a value of 97. When comparing strings, JavaScript compares these ASCII values. So, when you compare 'A' to 'a':

console.log('A' < 'a'); // Output: true

JavaScript says that 'A' is less than 'a', because 65 is less than 97.

The LocaleCompare Method: A Better Way to Compare Strings

While ASCII values work fine for most cases, they can result in unexpected behavior when dealing with non-English characters or when you want to sort words in a way that makes sense to human readers.

This is where the localeCompare method comes into play. This method compares two strings based on local language rules. Here's how you use it:

let str1 = "apple";
let str2 = "Banana";

console.log(str1.localeCompare(str2)); // Output: 1

The localeCompare method returns a number that tells us the order of the strings. If it returns a negative number, the first string comes before the second. If it returns a positive number, the first string comes after the second. If it returns 0, the strings are equal.

Conclusion: The Art of Comparing Strings

Congratulations on making it this far! You have now mastered the art of comparing strings in JavaScript. While it may seem like a small piece of the puzzle, understanding how to compare strings is a stepping stone to more complex operations, like sorting arrays of strings or matching patterns in text.

Remember, JavaScript, like any language, has its own unique quirks and features. It's essential to understand not just the 'how' but also the 'why' behind these features. Comparing strings is more than just seeing if 'A' is the same as 'a'; it's about understanding how your computer sees and interprets text.

So, the next time you're sipping your coffee and your computer successfully recognizes that "apple" is indeed the same as "apple", you'll know exactly what's happening behind the scenes. Happy coding!