Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to compare strings in JavaScript

JavaScript is a versatile and widely-used programming language that allows you to add interactivity to your web pages. One of the many things you might want to do with JavaScript is compare strings. In this blog, we'll take a deep dive into different approaches to compare strings in JavaScript, and we'll provide you with actual code examples to help you understand better.

What are Strings?

Before we start comparing strings, let's take a moment to understand what strings are in the context of programming. A string is a sequence of characters. In other words, it's a piece of text. In JavaScript, strings are enclosed in quotes, either single (') or double ("). Here's an example of a string:

let greeting = "Hello, world!";

Why Compare Strings?

There are several reasons why you might want to compare strings. You might want to check if two strings are equal to create a login system, or if one string comes before another to sort an array of names alphabetically. Comparing strings can help you determine the relationship between them and make decisions based on that information.

Basic String Comparison

Now that we know what strings are and why we might want to compare them, let's look at the simplest way to compare strings: using the equality (==) and identity (===) operators.

Equality Operator

The equality operator (==) checks whether two values are equal, and returns true if they are, or false if they're not. When comparing strings, this means that it checks whether the two strings have the same sequence of characters.

Here's an example:

let string1 = "apple";
let string2 = "apple";
let string3 = "banana";

console.log(string1 == string2); // true
console.log(string1 == string3); // false

Identity Operator

The identity operator (===) is similar to the equality operator, but it also checks whether the two values have the same type. Since we're comparing strings, this doesn't make a significant difference in most cases. However, it's generally recommended to use the identity operator when comparing values in JavaScript, as it can prevent unintended type coercion.

Here's an example:

let string1 = "apple";
let string2 = "apple";
let string3 = "banana";

console.log(string1 === string2); // true
console.log(string1 === string3); // false

Case Sensitivity

When comparing strings using the equality or identity operators, JavaScript is case-sensitive. That means that the comparison will return false if the strings have the same characters but different cases.

Here's an example:

let string1 = "apple";
let string2 = "Apple";

console.log(string1 == string2); // false

If you want to compare strings in a case-insensitive way, you can convert both strings to the same case before comparing them, using either the toLowerCase() or toUpperCase() methods.

Here's an example:

let string1 = "apple";
let string2 = "Apple";

console.log(string1.toLowerCase() == string2.toLowerCase()); // true

Comparing Strings with localeCompare()

If you want to compare strings in a way that's more suitable for sorting or displaying text in a user interface, you can use the localeCompare() method. This method compares two strings according to the rules of the current locale, which means that it takes into account language-specific rules for sorting characters.

Here's an example:

let string1 = "apple";
let string2 = "banana";

console.log(string1.localeCompare(string2)); // -1

In this case, localeCompare() returns -1, which means that string1 comes before string2 in the sorted order. If localeCompare() returns 0, the two strings are equal, and if it returns 1, the first string comes after the second one.

You can also use localeCompare() to sort an array of strings:

let fruits = ["banana", "apple", "kiwi", "mango"];

fruits.sort((a, b) => a.localeCompare(b));

console.log(fruits); // ["apple", "banana", "kiwi", "mango"]

Conclusion

In this blog, we've explored different ways to compare strings in JavaScript, including basic comparisons with the equality and identity operators, case-insensitive comparisons, and using the localeCompare() method for language-aware comparisons. Remember that the method you choose depends on your specific use case and requirements.

As you continue learning programming, you'll encounter many more situations where comparing strings is necessary. Keep practicing and trying out different examples to become more comfortable with string comparison in JavaScript. Happy coding!