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 中比较字符串时,为什么一个字符串比另一个大?
基础教程推荐
- Bootstrap 模态出现在背景下 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
