Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

what's the result of 1 + undefined

Writer Mia Lopez
1 + undefined = ? 
  1. first, String(undefined) get string "undefined"
  2. 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

7

3 Answers

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.

3

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?

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy