简体   繁体   English

为什么Number <String在JavaScript中返回true?

[英]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值为83N 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值


Second answer based on OP's edited question: 基于OP编辑问题的第二个答案:

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 <或任何类似的运算符用于表达式,而NumberString是函数而不是表达式。 However @Pointy explained on how Number < String worked and gave you results. @Pointy解释了Number < String工作原理并给出了结果。

More insight on comparison operators 更深入地了解比较运算符

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 以下是比较的规则/功能,请在此处阅读更多内容

  • Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions. 两个字符串在相应位置具有相同的字符序列,相同的长度和相同的字符时严格相等。
  • Two numbers are strictly equal when they are numerically equal (have the same number value). 两个数字在数值上相等(具有相同的数值)时严格相等。 NaN is not equal to anything, including NaN. NaN不等于任何东西,包括NaN。 Positive and negative zeros are equal to one another. 正负零彼此相等。
  • Two Boolean operands are strictly equal if both are true or both are false. 如果两个布尔操作数都为真或两者都为假,则它们严格相等。
  • Two distinct objects are never equal for either strict or abstract comparisons. 对于严格或抽象的比较,两个不同的对象永远不会相等。
  • An expression comparing Objects is only true if the operands reference the same Object. 仅当操作数引用相同的Object时,才会使用比较Objects的表达式。
  • Null and Undefined Types are strictly equal to themselves and abstractly equal to each other. 空和未定义类型严格等于它们自己并且抽象地彼此相等。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM