Why is one string greater than the other when comparing strings in JavaScript?(在 JavaScript 中比较字符串时,为什么一个字符串比另一个大?)
问题描述
我从一本书中看到了这段代码:
I see this code from a book:
var a = "one";
var b = "four";
a>b; // will return true
但它没有提到为什么一"大于四".我试过 c = "a"
并且它小于 a 和 b.我想知道 JavaScript 如何比较这些字符串.
but it doesn't mention why "one" is bigger than "four". I tried c = "a"
and it is smaller than a and b. I want to know how JavaScript compares these strings.
推荐答案
因为在许多编程语言中,字符串是比较的 按字典顺序.
Because, as in many programming languages, strings are compared lexicographically.
您可以将其视为字母顺序的更高级版本,区别在于该字母顺序仅涵盖 a
到 z
的 26 个字符.
You can think of this as a fancier version of alphabetical ordering, the difference being that alphabetic ordering only covers the 26 characters a
through z
.
这个答案是对java 问题,但逻辑完全一样.另一个不错的:字符串比较Logic".
This answer is in response to a java question, but the logic is exactly the same. Another good one: String Compare "Logic".
这篇关于在 JavaScript 中比较字符串时,为什么一个字符串比另一个大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 JavaScript 中比较字符串时,为什么一个字符串比另一个大?


基础教程推荐
- 我什么时候应该在导入时使用方括号 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06
- 动态更新多个选择框 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01