Junior — Middle
Is a variable environment global or limited in scope?
sobes.tech AI
Answer from AI
A variable named environment can be either global or local, depending on where and how it is declared.
- If
environmentis declared outside of functions and classes (for example, at the module or file level), then it is global and accessible throughout the module or program. - If
environmentis declared inside a function, method, or block, then it has local scope and is only accessible within that block.
For example, in JavaScript:
let environment = 'production'; // global variable
function test() {
let environment = 'development'; // local variable, shadows the global one
console.log(environment); // 'development'
}
console.log(environment); // 'production'
Therefore, to determine the scope of the environment variable, you need to look at where it is declared.