The optimum way to compare strings in JavaScript

There is method by which we can compare the two strings i.e, String localeCompare() method. This method compares two strings in the current locale. The current locale is based on the language settings of the browser. This method returns a number that tells whether the string comes before, after, or is equal to the compareString in sort order.

Code:
<!DOCTYPE html>
<html>
 
<head>
    <title>JavaScript Optimum way to compare strings</title>
</head>
 
<body style="text-align:center;" id="body">
    <h1 style="color:green;"> 
            GeeksforGeeks 
        </h1> String_1:
    <input type="text" id="text1" name="tname1">
    <br>
    <br> String_2:
    <input type="text" id="text2" name="tname2">
    <br>
    <br>
    <button onclick="gfg_Run()"> Compare </button>
    <p id="GFG_DOWN" style="color:green;
              font-size: 20px;
              font-weight: bold;"> </p>
 
 
    <script>
        var str1 = document.getElementById("text1");
        var str2 = document.getElementById("text2");
        var el_down = document.getElementById("GFG_DOWN");
     
        function gfg_Run() {
            var a = str1.value;
            var b = str2.value;
            var ans = a.localeCompare(b);
            var res = "";
            if(ans == -1) {
                res = '"' + a + '" comes before "' + b + '"';
            } else if(ans == 0) {
                res = 'Both string are same';
            } else {
                res = '"' + a + '" comes after "' + b + '"';
            }
            el_down.innerHTML = res;
        }
    </script>
</body>
 
</html>

This will compare the two string and give which will come first.

Leave a comment

Your email address will not be published. Required fields are marked *