0

Can someone help me understand why the string values entered in an html form are returning as a number when I use typeof, even when the original value is very specifically a string?

Below is a link to a pic of two values input first as random text (yyy) then as as a number (999), followed by their "types" before and after passing the values through the Number() function. Before they pass through the Number() function both are strings, as expected.

However, after passing the intentional string (yyy) through the Number() function I expected that "if you pass in a string with random text in it, you’ll get NaN, an acronym for 'Not a Number.' "

But, instead, it's saying that both (999)'s and (yyy)'s values are of the type "number".

image of form and console log

Queue
  • 1
  • 2
  • `typeof NaN === 'number`'. You check for `NaN` with `isNaN`, not `typeof`. – Ry- Aug 29 '20 at 19:24
  • ...but if I pass a string through a ```Number()``` function, for example: ```Number("this value is a random string of text and not a number")``` it's supposed to return as NaN --as far as I understand. Because it didn't ```isNaN()``` didn't work when I attempted to use it for validation. I ended up having to put the values into a mathematical equation ```(value / 1)``` in order to get ```isNaN``` to function the way I expected it to. – Queue Aug 29 '20 at 19:59
  • It does return `NaN`. Without seeing the full code you’re using to try it it’s not clear where the issue is, but if you open up your console and run `isNaN(Number("this value is a random string of text and not a number"))` it should be `true`. – Ry- Aug 29 '20 at 20:05
  • Yep, I see what you're saying, now. Thank you! – Queue Aug 29 '20 at 20:08

0 Answers0