Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda