admin管理员组文章数量:1193774
1 + undefined = ?
- first, String(undefined) get string "undefined"
- second, 1 + "undefined" = "1undefined"
what's wrong?
I run it in chrome console ,it return NaN
.
can you pls explain the result?
I think it should be "1undefined". tks
1 + undefined = ?
- first, String(undefined) get string "undefined"
- second, 1 + "undefined" = "1undefined"
what's wrong?
I run it in chrome console ,it return NaN
.
can you pls explain the result?
I think it should be "1undefined". tks
Share Improve this question edited Nov 24, 2013 at 1:41 PeeHaa 72.6k60 gold badges194 silver badges264 bronze badges asked Feb 20, 2013 at 10:28 loopinglooping 1,1613 gold badges11 silver badges20 bronze badges 7 | Show 2 more comments4 Answers
Reset to default 18NaN
is the result of a failed Number
operation.
1 + undefined // NaN
"1" + undefined // "1undefined"
1 + "" + undefined // "1undefined"
1 + ("" + undefined) // "1undefined"
typeof NaN // "number"
typeof undefined // "undefined"
NaN === NaN // false (it's not reflexive!)
undefined === undefined // true (it's reflexive)
NaN.toString() // "NaN"
NaN
means Not a Number where a number was expected. Any Number operation with NaN
will result in NaN
as well.
You expect a string concatenation, but this will only happen if you have at least one string. And in your example nothing is a string. 1
is not a string and undefined
is not a string.
1 + undefined = NaN
When you do 1 + "undefined"
you concatinate the 1 to the String "undefined"
resulting in the string "1undefined"
undefined is nothing or like Null in other languages (variable is not set)
In Javascript null is an expected absense (set to null somewhere) of a value and undefined is an unexpected absense of a value (never set)
What do you want to accomplish?
var charsLeft = 35 - (currentWord?.length ?? 0)
Thought that this code would come handy to someone.
本文标签: javascriptwhat39s the result of 1undefinedStack Overflow
版权声明:本文标题:javascript - what's the result of 1 + undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738489953a2089646.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
1
norundefined
are strings, so they're coerced to numbers.Number(undefined)
isNaN
, and1 + NaN
is stillNaN
. – Frédéric Hamidi Commented Feb 20, 2013 at 10:33undefined
gets converted to a string? It doesn't. – Felix Kling Commented Feb 20, 2013 at 10:34