"If" and "else" and "return" , anyone to explain please :)

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(true))

Why in the code above, the function does not return “No, that was false” even though the last return is return "No, that was false"; , and i am not using the else statement for the function to return the other value like for example in the code below:

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

}
console.log(trueOrFalse(true))

Does the if statement have the priority or what, can someone explain please?
and What is the difference between writing with else and without else

Hi @bedward

From what I can see in the code your code is correct in case of false then it will return “No, That was false” but from what I can see as you are passing the true condition in the log so you are not getting the false statement.
If possible attach your challenge it might help us understand more about what problem you are facing if the challenge is failing. :wink:

1 Like

As for the if else and return as per your code

if the condition is true then it returns the string and closes the function. if the IF condition fails then it returns the false statement also it will be returned in case of values other than if condtions required. :wink:

Thank you for giving your time, i understood now thanks anyway.
Solution: The return statement stops the execution of a function and returns a value from that function.
That’s why the function doesn’t keep going and return the “No, that was false”.

2 Likes

Hello @bedward
The return statement below the if block is considered to act as the else statement if the ELSE keyword is not provided.
The code below is the same

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(true))

it is the same as

function trueOrFalse(wasThatTrue) {

  // Only change code below this line

  if (wasThatTrue) {

    return "Yes, that was true";

  } else{

  return "No, that was false";
}

  // Only change code above this line

}

console.log(trueOrFalse(true))

We can also chain a if else while using the if statement to indicate a if else

function trueOrFalse(wasThatTrue) {

  // Only change code below this line

  if (wasThatTrue == true) {

    return "Yes, that was true";

  }
if(wasThatTrue == false){

return "No,ThatIsNotTrue"

}

  return "No, value passed in the function";

  // Only change code above this line

}

console.log(trueOrFalse(false))     //No,ThatIsNotTrue

The code above checks if the argument passed in the trueOrFalse Function is true then Yes,ThatWasTrue is Logged else if the argument is false then No,ThatIsNotTrue is Logged else log No, value passed in the function.

Careful. The original function actually tests for Truthiness or Falsiness, and using a == significantly changes the behavior of the function:
[https://repl.it/repls/ConsiderateWickedIrcbot#index.js]

1 Like

when you call the function

console.log(trueOrFalse(true)) 

// here you are setting the var wasThatTrue = true so it returns
// ```
return “Yes, that was true”;