0

this is a beginner JS question regarding NaN in an IF statement. I have code below that prompts user to enter a number, and the program will generates a random number between 1 and the user's input. However, IF the user doesn't enter a number, it will prompt user to retry and enter a valid number instead.

const userInput = prompt("Please enter a number.");
const userNumber = parseInt(userInput);

if (userNumber) {
  const randomNumber = Math.floor( Math.random() * userNumber ) + 1;
  console.log(`${randomNumber} is a number between 1 and ${userNumber}`);
} else {
  console.log(`Please enter a valid number.`);
}

The question I have is the userNumber section of the IF statement. The condition is basically asking IF userNumber IS a number, then output the random number. But why is it simply written with the variable in the condition, and not a condition such as if (userNumber != NaN) or if (userNumber == number)? Why is it just the variable in the condition?

To me, reading that line of code I'm thinking: "... if userNumber WHAT? There's nothing after userNumber, so what is the condition?"

Pete
  • 117
  • 3
  • 2
    That's not what `if (userNumber)` does. It checks if it is *truthy*. Since it's the return value of `parseInt`, it will be a `number` type, and of that type only two values are not *truthy*: `NaN`... and `0`. If the user inputs `0`, it will dismiss it as not a valid number. You might also want to handle negative numbers better... – Niet the Dark Absol Feb 27 '21 at 22:34
  • If you want to specifically test for `NaN`, use `if( isNaN(userNumber))` – Niet the Dark Absol Feb 27 '21 at 22:36

4 Answers4

2

You are right to be skeptical,

if(userNumber) ...

will test for userNumber to be "truthy", i.e. it will test whether it has a value different from 0. The input 0 would strictly speaking be numeric and in that respect the if-condition might not test as intended.

It could, on the other hand, also be seen as a shortcut for writing:

if (!isNaN(userNumber) && userNumber!=0)...
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
0

Mozilla Says;

The isNaN() function determines whether a value is NaN or not. Note, coercion inside the isNaN function has interesting rules; you may alternatively want to use Number.isNaN(), as defined in ECMAScript 2015.

function milliseconds(x) {
  if (isNaN(x)) {
    return 'Not a Number!';
  }
  return x * 1000;
}

console.log(milliseconds('100F'));
// expected output: "Not a Number!"

console.log(milliseconds('0.0314E+2'));
// expected output: 3140

More Details

0

In Javascript conditional statements check for truthy values and does type coercion. You dont need to explicitly do comparison in If statement.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else

https://developer.mozilla.org/en-US/docs/Glossary/Truthy

Gowtham Raj J
  • 937
  • 9
  • 26
0

When console logs NaN is the randomNumber variable.
Math method return NaN when you pass undefined as an argument.

const userNumber = undefined;
const randomNumber = Math.floor( Math.random() * userNumber ) + 1;
console.log(`${randomNumber} is a number between 1 and ${userNumber}`);

The conditions triggers with userNumber, be both numbers or words, because userInput contains a truthy value:

Falsy values: undefined, null, 0, "", false
Truthy values: 1, "0", "string", true

sonEtLumiere
  • 4,461
  • 3
  • 8
  • 35