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()