Problem with using if in js

Hi, I am a fresher in learning Java and to be honest, learning by self is really hard. In If lesson (https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-conditional-logic-with-if-statements) there is an example of using it. It says that: since argument (true) === true so the condition is met. But when I change the argument from (true) to any except (false) suggest (“test”) or (“checking”), the condition for true still is executed. I will cite the example written in above link as below:

function test (myCondition) {
  if (myCondition) {
     return "It was true";
  }
  return "It was false";
}
test(true);  // returns "It was true"
test(false); // returns "It was false"
test("test"); // returns "It was true"

I tried another way to test as: using console.log(“test”=true) and it returns false.
Anyone can enlighten me this struggling?
Thank you so much for any comment because I stuck for a while.

Welcome, dangth.

I suggest you have a look at the documentation on the subject of Truthy Values

Hope this helps

1 Like

Note that console.log("test" = true) must return false because = is the assignment operator and assigning the Boolean value true to a string will fail. You perhaps want a comparator == or ===.

But, as Sky said, you really want to toolk into truthiness.

1 Like

Hi Sky and Jeremy, I got it now :slight_smile: it’s exactly what i need. Thanks a lot!

1 Like