ScanSkill

localeCompare

The localeCompare() is used to compare two strings.

Syntax

string1.localeCompare(string2)

Example

const stringOne = 'abc';
const stringTwo = 'xyz';

console.log(stringOne.localeCompare(stringTwo)); // Prints: -1
console.log(stringTwo.localeCompare(stringOne)); // Prints: 1

const stringThree = 'abc';

console.log(stringOne.localCompare(stringThree)); // Prints: 0

The localeCompare() method compares two string the first one being the string comparing to and the second one wrapped by bracket being compare to string. It returns three values [ -1, 0, 1 ], if the string is smaller than the compare to string it returns -1, if the string is equal then 0, is returned. 1, is returned if the string is greater than the compare to string.