what's the result of 1 + undefined

前端 未结 3 1181
天涯浪人
天涯浪人 2020-12-17 01:11
1 + undefined = ?  
  1. first, String(undefined) get string \"undefined\"
  2. second, 1 + \"undefined\" = \"1undefined\"

what

相关标签:
3条回答
  • 2020-12-17 01:54

    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?

    0 讨论(0)
  • 2020-12-17 02:00

    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.

    0 讨论(0)
  • 2020-12-17 02:02

    NaN 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.

    0 讨论(0)
提交回复
热议问题