JavaScript Program to Compare Two Strings

Updated on September 30, 2024
Compare Two Strings header image

Introduction

Comparing two strings in JavaScript is a fundamental operation frequently performed in web development, data validation, and many other programming tasks. It enables developers to analyze, sort, or manipulate text-based data according to specific requirements. Whether it’s checking for equality, determining sort order, or finding differences, string comparison is a versatile tool in JavaScript programming.

In this article, you will learn how to compare two strings in JavaScript using different methods. Discover how to use equality operators and locale-based comparisons to effectively handle and evaluate strings in various scenarios.

Basic String Comparison with Equality Operators

Check for Exact Equality

  1. Use the equality operator (===) to compare two strings.

  2. This comparison is case-sensitive, ensuring both strings are identical in content and casing.

    javascript
    let string1 = "Hello";
    let string2 = "Hello";
    let result = string1 === string2;
    console.log(result);  // Outputs: true
    

    In this code snippet, string1 and string2 are exactly the same, which results in true.

Case-Sensitive Comparison

  1. To highlight the case sensitivity, compare two strings with different cases using the same equality operator.

    javascript
    let string1 = "hello";
    let string2 = "Hello";
    let result = string1 === string2;
    console.log(result);  // Outputs: false
    

    Here, the difference in casing between string1 and string2 results in false.

Locale-Aware Comparison using localeCompare()

Basic Usage of localeCompare()

  1. Understand that localeCompare() performs a comparison based on local language settings.

  2. It returns:

    • 0 if both strings are equal,
    • -1 if the reference string (the one calling the method) comes before the compared string lexicographically,
    • 1 if it comes after.
    javascript
    let string1 = "apple";
    let string2 = "banana";
    let result = string1.localeCompare(string2);
    console.log(result);  // Outputs: -1
    

    In this example, since "apple" comes before "banana" lexicographically, localeCompare() returns -1.

Comparing with Accent and Case Sensitivity

  1. Use the options object in localeCompare() to handle diacritics and case sensitivity effectively.

    javascript
    let string1 = "café";
    let string2 = "cafe";
    let options = { sensitivity: "accent" };
    let result = string1.localeCompare(string2, undefined, options);
    console.log(result);  // Outputs: 1
    

    This code snippet shows how localeCompare() can differentiate between "café" (with an accent) and "cafe" when the sensitivity is set to "accent".

Conclusion

String comparison in JavaScript offers versatile tools through simple equality checks and more nuanced, locale-sensitive methods like localeCompare(). Whether enforcing case-sensitive equality or sorting strings in accordance with local language rules, JavaScript provides the necessary functionality to handle these tasks efficiently. Applying these techniques allows for more robust and user-friendly string manipulation and validation in your applications. Explore these methods to enhance your programming toolkit and ensure your text-processing logic meets various needs.