JavaScript - localeCompare() method

revision:


Category : string

The localeCompare() method compares two strings in the current locale. The localeCompare() method returns sort order -1, 1, or 0 (for before, after, or equal). The current locale is based on the language settings of the browser.

Syntax :

        string.localeCompare(compareString)
    

Parameters:

compareString : required. The string to compare with.

Examples:

        <p>localeCompare() returns one of 3 numbers indicating the sort order.</p> 
        <ul>
            <li>-1 if sorted before</li>
            <li>1 if sorted after</li>
            <li>0 if equal</li> 
        </ul>
        <p>Compare "ab" with "cd":</p>
        <p id="demo"></p>
        <script>
            let text1 = "ab";
            let text2 = "cd";
            let result = text1.localeCompare(text2);
            document.getElementById("demo").innerHTML = result;
        </script>
    

Practical examples

example: compare "ab" with "cd".

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

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

Compare "ab" with "cd":

code:
                    <div>
                        <p>localeCompare() returns one of 3 numbers indicating the sort order.</p> 
                        <ul>
                            <li>-1 if sorted before</li>
                            <li>1 if sorted after</li>
                            <li>0 if equal</li> 
                        </ul>
                        <p>Compare "ab" with "cd":</p>
                        <p id="compare-1"></p>
                        <p id="compare-2"></p>
                    
                    </div>
                    <script>
                        let text1 = "ab";
                        let text2 = "cd";
                        document.getElementById("compare-1").innerHTML = "text 1 : " + text1 + " ; " + "text 2 : "+ text2;
                        let result = text1.localeCompare(text2);            
                        document.getElementById("compare-2").innerHTML = "result : " + result;
                                    
                    </script>
                

example: compare "ab" with "cd".

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

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

Compare "cd" with "ab":

code:
                    <div>
                        <p>localeCompare() returns one of 3 numbers indicating the sort order.</p> 
                        <ul>
                            <li>-1 if sorted before</li>
                            <li>1 if sorted after</li>
                            <li>0 if equal</li> 
                        </ul>
                        <p>Compare "cd" with "ab":</p>
                        <p id="compare-3"></p>
                        <p id="compare-4"></p>
                    
                    </div>
                    <script>
                        let text3 = "cd";
                        let text4 = "ab";
                        document.getElementById("compare-3").innerHTML = "text 3 : " + text3 + " ; " + "text 4 : "+ text4;
                        let result1 = text3.localeCompare(text4);            
                        document.getElementById("compare-4").innerHTML = "result : " + result1;
                                    
                    </script>
                

example: compare "ab" with "ab".

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

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

Compare "ab" with "ab":

code:
                    <div>
                        <p>localeCompare() returns one of 3 numbers indicating the sort order.</p> 
                        <ul>
                            <li>-1 if sorted before</li>
                            <li>1 if sorted after</li>
                            <li>0 if equal</li> 
                        </ul>
                        <p>Compare "ab" with "ab":</p>
                        <p id="compare-5"></p>
                        <p id="compare-6"></p>
                    
                    </div>
                    <script>
                        let text5 = "ab";
                        let text6 = "ab";
                        document.getElementById("compare-5").innerHTML = "text 5 : " + text5 + " ; " + "text 6 : "+ text6;
                        let result2 = text5.localeCompare(text6);            
                        document.getElementById("compare-6").innerHTML = "result : " + result2;
                                    
                    </script>