Skip to content
Advertisement

Boolean logic interpretation in google chrome console

I’m having difficulties figuring out why the code below doesn’t work as expected:

const userInput = prompt("Enter something");

if (userInput) {
    console.log("TRUTHY");
} else {
    console.log("FALSY");
}

I keep getting “TRUTHY” no matter what I do. I understand the logic of this code and even when running the source file from the class I’m not getting the expected output.

I should get “FALSY” whenever the input is: 0, null, undefined, an empty string or NaN.

What am I doing wrong? Thank you.

Edit 1: Turns out I was getting ahead of myself. The code should in fact return “TRUTHY” unless you input an empty string.


Advertisement

Answer

Which browser are you using? because when I run this code on ms edge, it returns FALSY when I enter nothing. Also, userInput is set to a string type by default, and the string “0” is true as it contains something. You’ll have to use parseInt() to convert the value to an integer, though that doesn’t look like what you want to do. Consider looking for syntax errors, and check if your browser is up to date.

Advertisement