Recursive prompt returns undefined

I noticed something quite odd this morning when I was trying to implement an endless prompting system.

The following code returns 2 different outputs under 2 scenarios:

Scenario 1: Enter number on first prompt and ok

Output 1:

number that was entered

Scenario 2: Cancel first prompt then enter number and ok

Output 2:

undefined

I'm perplexed as to why this is happening. Firstly, how does this return undefined when I am checking for that in the if statement? Second, I was under the impression that in JavaScript undefined means a variable has been declared but has not yet been assigned and in this case I am assigning the