Switch Statement to pull true and false from 2 variables

Im trying to figure out how to get the case to ask whether userName is truthy or falsy. and empty string is suppose to bring back false but if i just “userName === userName” it always comes back true because its not comparing the value inside of inside so im unable to get it to check the value of whether userName is truthy or falsy in terms of its value.

switch (true) {

case userName === true && userQuestion === true :

console.log(`${userName} wants too know...${userQuestion}`);

break;

case 1 === 2 && 1 === 1 :

console.log(`The User has wants to know...${userQuestion}`);

break;

case true && false :

console.log(`${userName} you need to ask a question!`);

break;

deafult:

console.log('You have not asked a question.');

break;

}

i tried different things but obviously 1 === 2 will always be false and 1 === 1 will always be true so its not really determining if userName and userQuestion come back true or false.

This will always return true, since the values are the same in both. It doesn’t matter if the values are truthy or falsey.

1 === 2

is false.

1 !== 2

is true.


If you want to see if a variable is falsey, you can convert it to type Boolean by using:

let x = "someStrng";
console.log(!!x);
//prints true

This works because !x negates x and turns it into a boolean. So if x is 3, then !x turns 3 into false. The second ! reverses this (reverses true to false, in this case), to get the correct boolean value. Here is an article on this.

Whoaaa, okay i didn’t know using more than 1 was possible. i tried 1 ‘!’ earlier but didn’t know how to make the 2nd condition check for false/true as well. that’s incredible. Another solved question by you, Thank you.

1 Like