Parameter of Boolean function

can a parameter of a function ‘Boolean()’ be an input of a user typing?
thank you

We are going to need far, far more information to be able to deliver a coherent answer.

it is not a simple question then, ok.

It actually can be a simple question: if you do

console.log( Boolean( prompt( "type something!") ) );

That will let the user type into a dialog, and then take the data they type and convert it to a Boolean, showing you the true or false in the console. Boom done.

But your question is vague, in that what is the user input? Whatever type of data you pass into Boolean will coerce by a very strict set of rules.

For numbers, 0 is considered “falsy”. Any other number is “truthy,” or will coerce to true.

Strings, an empty string coerces to false. Any other string, true.

Sooo…what’s your user input? Can’t give you a clearer answer with a vague question.


It will always give a truthy value if you type something. Only an empty string would be false. All inputs will return strings (prompt, DOM input elements) and it is only if you give no input that it will be false. This isn’t really relevant unless you query for the input value outside an input or change event, or use a form submit, or a prompt.

Sometimes it can be good to be explicit about it, but for the most, you won’t actually use Boolean() but just let the value get coerced.

let userInput;

while(!userInput) {
  userInput = prompt('Please enter something')

as you can see here, i typed 0 which supposed to evaluates to false in boolean , it instead it gave a true value:

1 Like

Ahhh, but if you look on the DevDocs you’ll see that prompt returns a string.

You type 0, but javascript reads "0". Which Boolean sees is not an empty string, which makes it truthy.

Again, what is your intended use-case? Will the user type a number at a prompt and you try to determine the Boolean-ness of that number? You haven’t yet explained your particular situation, so as i said, any help you get will be vague.

that was my doubt, that whatever I input it will be a string.
thank you.

1 Like

no i was just wondering as i got to the boolean level i the curriculum of freecodecamp, i wanted to know how inputs are treated by a boolean function.

1 Like

Again with me suggesting DevDocs devdocs is a great site for referencing multiple documentation collections. But this article details how Boolean coerces any type of data.

Then the question becomes how the input data is coming in. All html inputs return strings, for example.

will I just found out that they can be coerced by the if statement with an equality operator, in the dev tool it works, if the input is a number console turn it into a string the comparison coerces it into a number again, and the if statement returns true. I noticed that if I add quotes to the number, it returns false: i assumed that the if statement removes only the first string quotes giving by the developer tool.

1 Like

Depends on which equality comparator you use. If you use == then yes, if the things being compared are not of similar types, javascript will attempt to match their types if it can.

If the strict comparator (===) is used, both type and value must match. No coercion is attempted with strict equality.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.