728x90
✨ 문자의 정렬 순서를 비교를 통해 알아낼 수 있는 helper method
💻Example Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// String.prototype.localeCompare() | |
console.log('a'.localeCompare('b')); | |
console.log('b'.localeCompare('b')); | |
console.log('c'.localeCompare('b')); | |
console.log('a'.localeCompare('g')); |

🧨
앞의 비교 문자가 뒤의 비교 문자보다 정렬 순서가 앞이라면 -1
앞의 비교 문자가 뒤의 비교 문자보다 정렬 순서가 같다면 0
앞의 비교 문자가 뒤의 비교 문자보다 정렬 순서가 뒤라면 1
이 출력되는 것을 확인할 수 있다.
String.prototype.localeCompare()
The localeCompare() method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.
developer.mozilla.org
'JavaScript > Built-in Method etc.' 카테고리의 다른 글
String.prototype.charAt() (0) | 2020.02.22 |
---|---|
Set (Object) (0) | 2020.02.20 |
Math.round() (0) | 2020.02.16 |
Math.abs() (0) | 2020.01.28 |
Math.PI (0) | 2020.01.28 |