Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda