function fun() {
G_Scope = "car";
}
console.log(G_Scope);
Error: ReferenceError: G_Scope is not defined
function fun() {
G_Scope = "car";
}
console.log(G_Scope);
Error: ReferenceError: G_Scope is not defined
The variable is defined if you call the function and is not defined until then. This code does work well.
function fun() {
G_Scope = "car";
}
fun();
console.log(G_Scope);
Although the code will run when you call the function, it’s not advisable to set global by dropping var/let/Const in the variable declaration. It could lead to all sorts of bugs.
G_scope is a local variable. It needs to be set outside like this to combat the above.
let G_scope;
function fun() {
G_Scope = "car";
}
fun()
console.log(G_Scope);
G_scope is initially given the value of undefined. Then it’s value is initiated within the function call fun() and available for use after the function call.
What you are missing here in your code is a simple function call