0
var userinput = prompt("Hello. Please enter a statement and I will repeat the last word");
if(typeof userinput !== "string"){
console.log("This is not a string.");
}

I am a complete newb and I was just trying to make a simple script that repeats the last word of a statement that you enter. I added this safeguard so that if you don't enter a string, it will display a message, but whenever I test the code with a number, it just ignores this part and still executes the else statement which is the code that finds the last word. I don't understand why this safeguard won't work.

Would really appreciate some help. Thanks

4
  • 3
    everything gets converted to a string with prompt. Commented Jul 5, 2013 at 15:55
  • Try this code: var userinput = +prompt("Enter number") then enter number, notice + Commented Jul 5, 2013 at 15:59
  • @GrijeshChauhan that will convert valid strings to NaN :) Not what he's looking for I think :) Commented Jul 5, 2013 at 16:01
  • @ben336 yes it will give NaN to a string if not a number, so this reason I said "enter number", yes but you are correct my comment can confuse him instead, OP is very new to JS. Commented Jul 5, 2013 at 16:04

7 Answers 7

3

Because prompt() returns a string unconditionally (or null). It does not try to parse what was entered and go "oh, hey, it's all digits. I'll just return an int instead". That means your test is basically meaningless... you'll never get anything OTHER than a string back, so your code will always take the 'else' path.

relevant doc: https://developer.mozilla.org/en-US/docs/Web/API/window.prompt

Sign up to request clarification or add additional context in comments.

Comments

0

"123" is still a string.

prompt returns a string or null. It doesn't matter what characters are entered.

Comments

0

Prompt returns a string. That string may be "2" but it is still a string.

you can test with a regex like this:

var reg = /^\d+$/;
if(reg.test(userinput)){
    console.log("This is a number.");
 }

to see if it is a number

Comments

0

Everything received from a user-input is a string at first. What you want to do is test if the input can be converted to a number of some sort.

Comments

0

What are you expecting it to do?

prompt() always returns a string, so userinput will always be a string, no matter what the user enters. If they don't enter anything, it will be a blank string, but still a string. If they enter a number, it will be a string that happens to contain digit characters.

So your typeof check will always fail.

If you want to check whether they entered anything, you could just check if it's got a non-blank value:

if(!userinput) {
     //user didn't enter anything.
}

Comments

0

The use of prompt() would always return a string. So, what you need is to test if it's also a number.

if(typeof userinput === "number") {
    console.log("This is also a number.");
} else {
    // only string
}

Alternatively, you could also use isNan() to check for "Not a Number".

Comments

0

Five people and counting have explained that "1" is a string... (as is "", an empty string), and prompt() returns strings...


...but, to achieve what it sounds like you're trying to do - give a different result if the string is a string that can be understood as representing a number - you could try this:

if( parseFloat(userinput) !== NaN ){

parseFloat() tries to translate the string into a number that may have a decimal place (a 'float'). If it can't, it returns NaN (Not a Number), a special entity type that evaluates as false (but so does 0 or -1, which are numbers, so we test !== NaN rather than just using if( parseFloat(userinput)){ which would test if it's something that evaluates as true).

As those docs explain, it's quite permissive, e.g. it will interpret "5 words in this string" as 5.0.

Or parseInt() if you only care about integers. Or, read up on regular expressions if you want to be strict about what type of input you allow in your strings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.