Hi everyone
for the purpose of learning and practicing the scope in javascript is it possible to create a global variable inside a function when it is called?,
I’ve tried this in my code editor and it gives me an error
function setBestCustomer() {
return var bestCustomer = "not bob"
unreachable code
}
Why not create the global variable outside the function and then assign it a value within the function? Yes, you can create a global variable inside a function, but in general it is considered best not to do that. Instead, always explicitly declare variables that you need for your application. In fact, it would be best to create as few global variables as possible unless they are constants that will not change throughout the application.