javascript - 为什么`"12:34" <"123:45"`等于`false`?
问题描述
有人可以解释为什么会出现这种情况'12:34' < '123:45'
吗false
?
console.log('12:34' < '123:45' ? 'true' : 'false')
解决方案
因为 和 的 ASCII3
码:
。我们知道字符串之间的比较将由字典规则完成。因此,由于第一个区别在两个字符串的第三个位置,因此 ASCII 码3
和:
将确定比较的结果。由于 的 ASCII 代码:
大于3
,因此您会看到 false 。请参阅下面的它们的 ASCII 代码。
console.log(":".charCodeAt(0));
console.log("3".charCodeAt(0));
推荐阅读
- css - 材质 UI 列表、ListItemText 中的 NO WRAP 挑战
- python - MongoDB(pymongo)查找并推送到嵌套数组
- vue.js - 如何在插件中访问 Vue $refs?
- xml - 在 Scala 中更改函数的返回类型
- typescript - Make the type of a field conditionally based on another field type
- html - How do you remove the gap between inner divs & container div?
- vb.net - 用鼠标移动后,PictureBox 会在 Form1 上留下重影吗?
- discord - 在频道上获取第一条消息
- php - 复式会计报告
- javascript - Why can't I refer to the inherited method from JavaScript prototype within method