1

I'm just getting back into coding and I'm curious as to why the checkNum(oddNum) function outside of the if statement is still being called?

If I'm understanding scoping correctly, shouldn't the checkNum function only be able to be called within the scope of the if statement?

Yes I am aware there is an easier way to do the check and the function is not required, but I am just practicing and trying to understand scoping. =D

let evenNum = 2; 
let oddNum = 3; 

if (evenNum < 5) {
    checkNum(evenNum);

    function checkNum(evenNum) {
        let isEven;
        isEven = evenNum % 2;
        if (isEven === 0) {
            console.log("The number is even");
        } else if (isEven != 0) { 
            console.log("The number is odd");
        } else {
            console.log("The number is greater than 5")
        }
    }
}

checkNum(oddNum);

EDIT:

As per javascript.info, this will not work, but the code seems to be similar?

let age = 16; // take 16 as an example

if (age < 18) {
  welcome(); // \   (runs)
  //  |
  function welcome() {  //  |
    alert("Hello!");    //  |  Function Declaration is available
  }                     //  |  everywhere in the block where it's declared
                        //  |
  welcome(); // /   (runs)

} else {

  function welcome() {
    alert("Greetings!");
  }
}

// Here we're out of curly braces,
// so we can not see Function Declarations made inside of them.

welcome(); // Error: welcome is not defined
Barmar
  • 741,623
  • 53
  • 500
  • 612
mynameisdlo
  • 489
  • 4
  • 10
  • 2
    The `function` declaration is scoped to the function, not the block. Block scoping only happens for variables declared with `let` or `const`. – Barmar Mar 23 '22 at 22:13
  • I've updated the post to reflect where my confusion lies. – mynameisdlo Mar 23 '22 at 22:25
  • I don't get an error with your second version. – Barmar Mar 23 '22 at 22:28
  • You would only get an error if the function is defined in an `if` or `else` block that's never executed. – Barmar Mar 23 '22 at 22:29
  • Weird. I get the error on the Javascript.info website after the first two function calls are executed, but it's not happening here. From what I understand you're saying that Block Scope doesn't apply to function declarations, correct? – mynameisdlo Mar 23 '22 at 22:33
  • I haven't used javascript.info before. Do you have a link to the place where you can execute code? – Barmar Mar 23 '22 at 22:36
  • 3
    Function declarations have block scope in strict mode, function scope in non-strict mode. So maybe javascript.info is using strict mode. – Barmar Mar 23 '22 at 22:37
  • 1
    Another dupe target would be [what-are-the-precise-semantics-of-block-level-functions-in-es6](https://stackoverflow.com/questions/31419897/what-are-the-precise-semantics-of-block-level-functions-in-es6) – ASDFGerte Mar 23 '22 at 22:39
  • https://javascript.info/function-expressions It's close to the bottom of the article near the summary @Barmar – mynameisdlo Mar 23 '22 at 22:43
  • javascript.info probably automatically has strict-mode. – ASDFGerte Mar 23 '22 at 22:44
  • From that page: **In strict mode, when a Function Declaration is within a code block, it’s visible everywhere inside that block. But not outside of it.**. Strict mode is not the default. – Barmar Mar 23 '22 at 22:44

0 Answers0