Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

101
Views
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()
7 months ago · Juan Pablo Isaza
1 answers
Answer question

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
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs