FizzBuzz "typeof input !== 'number'", theory question

Hey there! This is my first question on this forum. I hope this wasn’t asked previously. Here goes…

During the ‘Control Flow’ module, Exercise 3 named ‘FizzBuzz’ contains in it’s first ‘If statement’ this code:

=> If (typeof input !== ‘number’) return NaN;

My question is, why does ‘typeof input’ not equal “‘number’”?
Why can’t it not equal NaN?

Thanks in advance for your help! :v:

Hi! Are you asking why it can’t look something like this?

if (typeof input === 'NaN') return NaN;

The typeof operator returns one of only eight values, for example 'number' or 'boolean'. It never returns 'NaN'. NaN is not one of the types in JavaScript, but number is, so we use typeof to check if the input to the function is a number, and if not, we return NaN to indicate that we received something that is Not a Number.

I’m not sure if I answered your question!

1 Like

That was so much help thank you for the MDN links! I’m so reading all that. :sweat_smile:

I know this was asked 2 years ago but decided to answer.

If the particular input used is not a ‘number’ we are asking to return "Not a Number’

For example if the code says,

const output = fizzbuzz(true);

True which is a boolean, so we want it to return ‘Not a number’ to the console.

Therefore,

const output = fizzbuzz(true);
console.log(output);

function fizzbuzz(input) {
if (typeof input !== ‘number’)
return ‘Not a number’;
}

Hope this helps anyone with confusion.