localeCompare()  String  instance method JS Home  <<  JS Reference  <<  String  <<  localeCompare()

Description

Returns a number dependant on the sort order of a string comparison.

  • -1 if calling string comes before compared string.
  • 0 if calling string same as compared string.
  • 1 if calling string comes after compared string.

Syntax

Signature Description
aString.localeCompare(compareString)Returns a number dependant on the sort order of a string comparison.

Parameters

Parameter Description
compareStringThe string to compare to.

Examples

The code below compares two strings.


// Create two string variables.
var aString = 'acc';
var bString = 'abc';

// Compare strings (bstring is first in sort order).
alert(aString.localeCompare(bString));

Press the button below to action the above code:

Related Tutorials

JavaScript Basic Tutorials - Lesson 8 - Strings