Exercise
In non-strict mode, assigning a value to an undeclared variable automatically creates it as a global variable. This is considered bad practice because it pollutes the global scope.
function setName() {
name = 'JavaScript'; // no var, let, or const
}
setName();
console.log(name); // Output: JavaScript (created as global)
No. Function parameters are scoped to the function they belong to, just like variables declared with var inside the function. They cannot be accessed outside.
function greet(message) {
console.log(message); // Output: Hello
}
greet('Hello');
console.log(message); // ReferenceError: message is not defined
A variable declared with let inside a for loop is block-scoped to the loop body. It is not accessible outside the loop.
for (let i = 0; i < 3; i++) {
console.log(i); // Output: 0, 1, 2
}
console.log(i); // ReferenceError: i is not defined
Yes. Variables declared inside different functions have their own separate function scope. They do not conflict with each other even if they share the same name.
function first() {
var count = 10;
console.log(count); // Output: 10
}
function second() {
var count = 20;
console.log(count); // Output: 20
}
first();
second();
Declaring a variable twice with var in the same scope does not cause an error. The second declaration is ignored, but the assignment still takes effect.
var color = 'red';
var color = 'blue';
console.log(color); // Output: blue
A variable declared with var inside a while loop is not limited to the loop block. Since var is function-scoped (or global-scoped), it is accessible outside the while loop.
while (false) {
var loopVar = 'I exist';
}
console.log(loopVar); // Output: undefined (declared but never assigned)
When a local variable shares the same name as a global variable, the local variable takes priority within its scope. The global variable remains unchanged outside that scope.
var fruit = 'apple';
function pickFruit() {
var fruit = 'banana';
console.log(fruit); // Output: banana
}
pickFruit();
console.log(fruit); // Output: apple
No. Variables declared with const are block-scoped, just like let. They are only accessible within the block (curly braces) in which they are declared.
if (true) {
const language = 'JavaScript';
console.log(language); // Output: JavaScript
}
console.log(language); // ReferenceError: language is not defined
Variables declared at the top level of a script (outside any function or block) are in the global scope. They are accessible from anywhere in the program, including inside functions.
var appName = 'MyApp';
function showApp() {
console.log(appName); // Output: MyApp
}
showApp();
console.log(appName); // Output: MyApp
Yes. Every time a function is called, a new function scope is created. Variables declared inside that function are unique to that particular call and are destroyed when the function finishes executing.
function counter() {
var count = 0;
count++;
console.log(count);
}
counter(); // Output: 1
counter(); // Output: 1 (new scope each time, count resets)
counter(); // Output: 1