# JavaScript Strings Comparison

[![ebay](https://cdn.hashnode.com/res/hashnode/image/upload/v1614297416111/8DKzsZqno.jpeg)](https://www.ebay.com/e/_electronics/save-on-apple?mkcid=1&mkrid=711-53200-19255-0&siteid=0&campid=5338787315&toolid=20014&customid=&mkevt=1
)

---
### String Comparison
To check whether a string is greater than another, **dictionary** or **lexicographical** ordering is used.

> Lexicographical ordering is when strings are compared letter-by-letter.

See the example below:

```js
console.log( 'Z' > 'A' ); // true
console.log( 'Z' > 'a' ); // false
console.log( 'Bee' > 'Be' ); // true
```

In the example above, `'Z' > 'A'` but `'Z' < 'a'`. This is so because *a lowercase letter is always greater than the uppercase*

The algorithm to compare two strings is systematic.

```js
console.log('ridicules' > 'ridiculous'); // false
```

The example above is false because `ridicul === ridicul` but `ridicule < ridiculo` because `e` < `o`.

#### Characters unique code
Each character has its unique numeric code. The unique numeric code is what is actually used for comparison.

The syntax to find a character unique numeric code is

```js
str.codePointAt(pos)
```

See the example below:

```js
// code is also based on character case
console.log( "a".codePointAt(0) ); // 97
console.log( "A".codePointAt(0) ); // 65

console.log( "z".codePointAt(0) ); // 122
console.log( "Z".codePointAt(0) ); // 90
```

You can also get a character by its numeric code.

The syntax is shown below:

```js
String.fromCodePoint(code)
```

See the example below:

```js
console.log( String.fromCodePoint(97) ); // a
console.log( String.fromCodePoint(65) ); // A
```

Alternatively, you can add Unicode characters by their codes using `\u` followed by the hex code.

See the example below:

```js
// 90 is 5a in hexadecimal system
console.log( '\u005a' ); // Z
```

Let's iterate through each unique numeric code to see their characters.

See the example below:

```js
let str = '';

for (let i = 65; i <= 220; i++) {
  str += String.fromCodePoint(i);
}
console.log(str);
/*
ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜ
*/
```

In the example above, Capital characters go first, then a few special ones, lower case characters, etc.

Now it is obvious why `a > Z.`

> All modern browsers (IE10- requires the additional library [Intl.js](https://github.com/andyearnshaw/Intl.js/)) support the internationalization standard [ECMA-402](http://www.ecma-international.org/ecma-402/1.0/ECMA-402.pdf).

#### Locale String Comparison
The  [str1.localeCompare(str2)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare) returns an integer indicating whether `str1` is less, equal, or greater than `str2` according to the language rules.

Syntax

```
str1.localeCompare(str2)
```

- Returns `-num` if `str1 < str2`.
- Returns `+num` if `str1 > str2`.
- Returns `0` if `str1 === str2`.

See the example below:

```js
console.log( 'Österreich'.localeCompare('Zealand') ); // -1
```

---

[![Buy me a Coffee](https://res.cloudinary.com/bizstak/image/upload/v1610317510/ko-fi-trans_fr3su4.png)](https://www.buymeacoffee.com/techstackmedia)

---

[![ebay.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1614297578970/XTB8nUN-5.jpeg)](https://www.ebay.com/e/_electronics/direct-brands-certified-refurbished?mkcid=1&mkrid=711-53200-19255-0&siteid=0&campid=5338787315&toolid=20014&customid=&mkevt=1)

---
