[英]Why does Number < String returns true in JavaScript?
EDIT: I will rephrase my question, I type Number < String and it returns true, also works when I do typeof(2) < typeof("2"). 编辑:我将重新解释我的问题,我输入Number <String并返回true,当我输入typeof(2)<typeof(“2”)时也可以。
Number < String => true
typeof(2) < typeof("2") => true
I'm guessing it is the value of ASCII characters of each letter in Number and String but I am not sure if that is the reason this is returning true, and I want to know why does this happens, what processes or how does the interpreter gets to this result? 我猜它是Number和String中每个字母的ASCII字符的值,但我不确定这是否是返回true的原因,我想知道为什么会发生这种情况,什么过程或者如何解释得到这个结果?
First answer: 第一个答案:
The charCodeAt() method returns the numeric Unicode value of the character at the given index. charCodeAt()方法返回给定索引处字符的数字Unicode值。 Read here
在这里阅读
Now if you do not specify any index position then character at 0th index is considered. 现在,如果您未指定任何索引位置,则考虑第0个索引处的字符。 Now,
S
ASCII value is 83
and N
ASCII value is 78
. 现在,
S
ASCII值为83
, N
ASCII值为78
。 so, you are getting those number. 所以,你得到的那些号码。 Check here .
点击这里
And 78 < 83 => true
is obvious. 并且
78 < 83 => true
是显而易见的。
Try "String".charCodeAt(1)
and you will get 116
which is ASCII value of t
尝试
"String".charCodeAt(1)
,你将获得116
这是t
ASCII值
Frankly speaking your comparison Number < String
is "technically" incorrect because Less-than Operator <
or any similar operator is for expressions, and Number
and String
are functions and not expressions. 坦率地说,你的比较
Number < String
在技术上是“不正确的,因为Less-than Operator <
或任何类似的运算符用于表达式,而Number
和String
是函数而不是表达式。 However @Pointy explained on how Number < String
worked and gave you results. 但@Pointy解释了
Number < String
工作原理并给出了结果。
Comparison operators like <
works on expressions, read here . 像
<
在表达式上工作的比较运算符, 在这里阅读。 Typically, you should have a valid expression or resolved value for RHS and LHS. 通常,您应该具有RHS和LHS的有效表达式或已解析值。
Now this is the definition of expression, read more here - " An expression is any valid unit of code that resolves to a value. Conceptually, there are two types of expressions: those that assign a value to a variable and those that simply have a value. " 现在这是表达式的定义, 在这里阅读更多 - “ 表达式是解析为值的任何有效代码单元。从概念上讲,有两种类型的表达式:为变量赋值的那些和只有变量的变量。价值。 “
So, (x = 7) < (x = 2)
or new Number() < new String()
is a "technically" valid/good comparison, even this Object.toString < Number.toString()
but really not Object < Function
. 所以,
(x = 7) < (x = 2)
或new Number() < new String()
是一个“技术上”有效/良好的比较,甚至这个Object.toString < Number.toString()
但实际上不是Object < Function
。
Below are rules/features for comparisons, read more here 以下是比较的规则/功能,请在此处阅读更多内容
The result of 的结果
Number < String
is not the result of comparing the strings "Number" and "String", or not exactly that. 是不是比较串“数字”和“字符串”,或者不完全是结果。 It's the result of comparing the strings returned from
Number.toString()
and String.toString()
. 这是比较从
Number.toString()
和String.toString()
返回的字符串的结果。 Those strings will (in all the runtimes I know of) have more stuff in them than just the strings "Number" and "String", but those two substrings will be the first place that they're different. 那些字符串(在我所知的所有运行时)中都有更多东西,而不仅仅是字符串“Number”和“String”,但这两个子字符串将是它们不同的第一个字符串。
You can see what those actual strings are by typing 您可以通过键入来查看这些实际字符串是什么
Number.toString()
in your browser console. 在浏览器控制台中。
JavaScript does the following thing: JavaScript执行以下操作:
"String".charCodeAt(); => 83
"S".charCodeAt(); => 83
"String".charCodeAt(0); => 83
The method charCodeAt(a) gets the char code from position a . 方法charCodeAt(a)从位置a获取char代码。 The default value is 0
默认值为0
If you compare N > S you will get 78 > 83 => true 如果你比较N> S,你会得到78> 83 => true
For the complete String Javascript calculates the sum of all ASCII char codes. 对于完整的String,Javascript计算所有ASCII字符代码的总和。 So I can answer your question with yes.
所以我可以回答你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.