B
B
bormor2018-04-11 11:09:07
JavaScript
bormor, 2018-04-11 11:09:07

js type conversion. Why undefined + 1 = NaN?

It would seem that "+" starts the conversion to a string and concatenation.
I would expect
undefined + 1 = String(undefined) + 1 = 'undefined' + 1 = 'undefined1'
In fact
undefined + 1 = NaN
Why is that? How is the transformation done step by step?

PS Or there are no explicit lines here, so '+' starts the conversion to a number, i.e.
undefined + 1 = Number(undefined) + 1 = NaN + 1 = NaN
So?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Osher, 2018-04-11
@bormor

www.ecma-international.org/ecma-262/6.0/#sec-addit...
TLDR: in this particular case both operands are cast to a number. ToNumber(undefined) = NaN. NaN + 1 = NaN.

V
VoidVolker, 2018-04-11
@VoidVolker

Because those are the specifications of the language. Here is the full table: https://dorey.github.io/JavaScript-Equality-Table/
And don't duplicate questions, please - read the language specifications, everything is there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question