Basic JavaScript - Use Conditional Logic with If Statements

Apparently I don’t understand how true/false works? The below passes the tests, but the assignment was that the function should return “Yes, that was true” if and only if the argument is true, and false otherwise.

The opposite seems to be the case here. I can put literally anything EXCEPT false as the argument - a string, a number, whatever - and it evaluates as true. I’m stumped as to why, and would appreciate it greatly if anyone could help!

  **Your code so far**
function trueOrFalse(wasThatTrue) {
// Only change code below this line
if (wasThatTrue){
  return "Yes, that was true";
}
return "No, that was false";

// Only change code above this line

}
console.log(trueOrFalse("saywhat"))
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Use Conditional Logic with If Statements

Link to the challenge:

you have met truthy and falsy values!
When put in a boolean contest (which the condition of an if statement is, as it needs a boolean), all values are or truthy or falsy, a truthy value behaves like true, a falsy value like false
Falsy values are undefined, 0 (also -0 and 0n), "" (empty string), null, falsy, NaN, everything else is truthy. That’s why if you put pretty much anything, it behaves as true

2 Likes

Cool! So it’s just a feature of the language, then?
(I really love having this forum, btw. Without it I would just have fallen into a giant rabbit hole because I can’t NOT mess with the code. Googling usually helps, but not this time. So thank you so much for the quick reply!)

yep, there are specific challenges on this concept somewhere

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