Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
Function being called even not in same scope?

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!