Question

I'm very new to JS - only a couple days in.

Trying to write a very basic prompt evaluated by an if statement.

When I run the code below, the user is prompted, but the statement is never evaluated by the if statement.

Any help? -- I realize the answer is probably simple and obvious, but as a SUPER beginner, what do I do?

var bool = prompt("What is an example of a boolean?"); 

if (typeof(bool) === "boolean") {
    print("correct! that is a boolean"); 
    print(bool) ; 
}; 
Was it helpful?

Solution

In this case, assuming the user inputs something in the prompt, the type of the bool variable will always be a string. You'd rather check if the input compares to the string "true" or "false" etc., like this:

if (bool.toLowerCase() == "true" || bool.toLowerCase() == "false") {
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top