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

551
Visualizações
Confusion in global variable (var) and block variable (let , const) at node js

I understand that var is a global variable in node js and it can be accessed everywhere.

However I was confused by below examples

In below, global_1 can be accessed without confusion, as it is global variable.

var global_1 =1 

function2 = () => {

console.log('global_1 in function2: ' + global_1)

}

main = () =>{

console.log('global_1 in main: ' + global_1)

function2()

}

main()

But if I put my function2 inside a help.js ; it said global_1 is undefined ; isnt it that when I import helper function, the effect is same as the above code where I paste my function2 in the same file?

const helper = require('./helper'); 

var global_1 =1 

main = () =>{

console.log('global_1 in main: ' + global_1)

helper.function2()

}

main()

As for let and const, my understanding is that they can only be accessed within a {} 

But now, global_1 is still be able to be accessed by function2, even though global_1 is not defined inside function2. Isnt it only var can be accessed everywhere and let,const can only be access within {} ?

var global_1 =1 


or  let global_1 =1 

or  const global_1 =1 



function2 = () => {

console.log('global_1 in function2: ' + global_1)

}

main = () =>{

console.log('global_1 in main: ' + global_1)

function2()

}

main()
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I understand that var is a global variable in node js and it can be accessed everywhere.

You're wrong.

var inside a function declares a variable scoped to that function.

var outside of a function declares a variable scoped to that module.

(There's an edge case, that T.J. pointed out, where if you run the code in the global context (e.g. node < yourModule.js) instead of loading it as a normal module (node yourModule.js) then it won't be treated as a module and var outside a function will create a global).

But if I put my function2 inside a help.js ; it said global_1 is undefined ; isnt it that when I import helper function, the effect is same as the above code where I paste my function2 in the same file?

No.

The variables that are "in scope" are determined by where a function is defined, not by where it is called.

As for let and const, my understanding is that they can only be accessed within a {} 

No.

They can't be accessed outside of the block in which they are defined.

In your example they are defined outside of any explicit block, do the whole module is treated as the block for these purposes.


var a = 1;

function b () {
    var c = 2;
    let d = 3;
    
    if (a) {
        let e = 4;
        var f = 5;
        // a b c d e and f are available here
    }
    // a b c d and f are available here
    // e isn't because it is block scoped and this is outside the block
}

// a and b are available here.
// Everything else is scoped to either the function b or the block
// connected to the if statement inside it
about 4 years ago · Juan Pablo Isaza Relatório
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