首页 > 解决方案 > 为什么`"12:34" <"123:45"`等于`false`?

问题描述

有人可以解释为什么会出现这种情况'12:34' < '123:45'false

console.log('12:34' < '123:45' ? 'true' : 'false')

标签: javascriptstringcomparisonlexicographic

解决方案


因为 和 的 ASCII3:。我们知道字符串之间的比较将由字典规则完成。因此,由于第一个区别在两个字符串的第三个位置,因此 ASCII 码3:将确定比较的结果。由于 的 ASCII 代码:大于3,因此您会看到 false 。请参阅下面的它们的 ASCII 代码。

console.log(":".charCodeAt(0));
console.log("3".charCodeAt(0));


推荐阅读