Javascript - String Compare() Method

Introduction

localeCompare() compares one string to another and returns one of three values as follows:

Value Meaning
-1 If the string should come alphabetically before the string argument, a negative number is returned.
0 If the string is equal to the string argument,
1 If the string should come alphabetically after the string argument, a positive number is returned.

Demo

var stringValue = "A";
console.log(stringValue.localeCompare("a")); 
console.log(stringValue.localeCompare("A"));
console.log(stringValue.localeCompare("B"));   

function test(value) {
    var result = stringValue.localeCompare(value);
    if (result < 0){
        console.log("'A' comes before the string '" + value + "'.");
    } else if (result > 0) {
        console.log("'A' comes after the string '" + value + "'.");
    } else {/*www .  j ava2  s. c o m*/
        console.log("'A' is equal to the string '" + value + "'.");
    }
 }

test("a");
test("A");
test("B");

Result