Need help with Use Conditional Logic with If Statements excercise

I know I’m “coloring outside the lines” with this question but in this example if I change the trueOrFalse(true) function to something completely unexpected like “blue” I would expect a return of false. Instead I get "ReferenceError: can’t find variable: blue.

Can someone explain this to me?

Your code so far

// Example
function ourTrueOrFalse(isItTrue) {
  if (isItTrue) { 
    return "Yes, it's true";
  }
  return "No, it's false";
}

// Setup
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.

}

// Change this value to test
trueOrFalse(blue);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.1.2 Safari/603.3.8.

Link to the challenge:

true and false are Boolean values. JavaScript has multiple valid data types:

Boolean
Null
Undefined
Number
String
Symbol

Since blue is not one of these types, JS assumes it is a variable. Since blue was not declared (i.e using var or let or const), then you get the reference error.

Let’s say I have the following code:

var blue = "blue";

then assuming I had a function called myFunc which could use the value assigned to blue, then I could make the following call:

myFunc(blue);

Even, if I just declared a variable called blue without initializing it to a value (as seen below):

var blue;

I could still use it in a function call without getting an error, because JS automatically assigns the value undefined when a variable is declared without assigning a specific value.

Hope this helps explain why calling a function with just blue gave the reference error.

Thank you @RandellDawson. I now understand the first part of your answer. So Booleans (if statements) are only looking for returns of true or false? These are sort of inherently declared values in JavaScript?

if statements conditions have to evaluate to true or false.

For example,

var myVal = true;  // myVal contains a boolean data type
if (myVal) {
  console.log('if condition evaluated to true');
else {
  console.log('if condition evaluated to false');
}

The above would display the following in the console:

if condition evaluated to true

If had had:

var myVal = "fun";  // myVal contains a string data type
if (myVal) {
  console.log('if condition evaluated to true');
else {
  console.log('if condition evaluated to false');
}

The above would display the following in the console:

if condition evaluated to true

Where the following:

var myVal = 0;  // myVal contains a string data type
if (myVal) {
  console.log('if condition evaluated to true');
else {
  console.log('if condition evaluated to false');
}

would would display the following in the console:

if condition evaluated to false

If any of the above is confusing, then take a look at the following to understand how JS treats “truthy” values

@RandellDawson you had me until the last example. Sorry if I seem dense. Isn’t 0 a number data type where “0” is a string data type?

Is 0 always assumed to be equivalent to the data type of a boolean false? I should assume 1==true?

It is a number but the if condition evaluates it as if it were boolean. So when I write:

if (0) {

it evaluates to false (see the Truthy link I posted in the last reply). It has to do with the way JavaScript uses type coercion in Boolean contexts.

1 Like

Thank you so much for taking the time to explain that to me.