All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Compare Strings In JavaScript

Last Updated : Jul 1, 2023

How To Compare Strings In JavaScript

In this tutorial we will show you the solution of how to compare strings in JavaScript, in this guide, we'll learn how to compare strings using built-in Javascript functions and explore how they're implemented through an example.

The task is to compare two JavaScript strings as efficiently as possible.

Step By Step Guide On How To Compare Strings In JavaScript :-

The localeCompare() method compares two strings and gives a number indicating whether the second text comes before, after, or is equivalent to the first.

If the string comes before the compare string, it produces a negative number; if the string comes after the comparison string, it returns a positive number; and if both strings are equivalent, it returns 0.

Parameters: compareString is a string against which the reference string is compared in this case.

Return Values: A positive number is returned if the reference string is lexicographically greater than the compare string, a negative number is returned if the reference string is lexicographically smaller than the compare string, and zero (0) is returned if the compare and reference strings are equivalent.

The method's parameters are as follows:

  • compared string — the string against which the referred string is compared
  • locales — specifies which language should be used for comparison.
  • options — an object holds arguments that allow the method's behaviour to be further customised.

localeCompare() returns one of 3 numbers indicating the sort order.

  • -1 if sorted before
  • 1 if sorted after
  • 0 if equal

The following code explains how to compare two strings using the default method

<!DOCTYPE html>
<h1>Strings Comparision</h1>
<h2>The localeCompare() Method</h2>
<p>Compare "ab" with "xy":</p>
<p id="demo"></p>
let text1 = "ab";
let text2 = "xy";
let result = text1.localeCompare(text2);
document.getElementById("demo").innerHTML = result;
  1. To begin, type <! DOCTYPE html> to indicate that the file is in HTML format to the web browser.
  2. The <html> element, on the other hand, is used to indicate that HTML content is about to begin.
  3. The <head> tag now contains the information about web pages. The< title> element is used in this tag to provide a web page title. For example, the <head> and <title> tags are paired tags. As a result, both have the </head> and< /title> closing tags.
  4. Finally, the< body> element specifies the content of the web page. This is where all of the website's material will be written. We've inserted the script tag inside the body tag to incorporate our javascript code.
  5. In the above example, We're calling document.getElementById('demo') in the preceding example, thus it'll return< p id="demo"></p>. We have defined two strings called ab and xy. It will return -1 value as both the strings are not equal.
  6. Finally <body> and <html> tags are closed with< /body> and </html>, respectively.

Conclusion :-

The.localeCompare() method comes in handy when comparing texts in JavaScript, especially when sorting is involved. The localeCompare() method gives a number that shows whether a reference string is before, following, or identical to the given string.

We hope the above code will help you to compare strings in javascript. I hope this tutorial on how to compare strings in JavaScript helps you.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪