Compare Scopes of the var and let Keywords : what is the if checking?

Tell us what’s happening:
What is the if checking?
my guess is it doesn’t check anything. the code will run no matter what. On the flip side, if I change it to if(false), the code will NOT run.

I checked both MDN and W3School. They don’t have example where the condition is just a true or false. There is always a varible, a string or a number.

Many thanks

Your code so far


function checkScope() {
"use strict";
  let i = "function scope";
  if (true) {
    let i = "block scope";
    console.log("Block scope i is: ", i);
  }
  console.log("Function scope i is: ", i);
  return i;
}




function test (a){a==1||a==2?console.log("right"):console.log("wrong")};
test(1)
test(2)
test(3)
test(4)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Safari/605.1.15.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/compare-scopes-of-the-var-and-let-keywords

If statements always check if an expression evaluates to the value of true. Since you’re checking whether the value true === value true then it’ll run every time. As for the “purpose” of the if statement, it’s here to create a “block scope” within the “function scope”.

Interestingly, if you remove the if (true) and just leave the curly braces, the code will still work because the “block scope” still exists between those curly braces.

2 Likes