0
0
Javascriptprogramming~20 mins

Module scope in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Module Scope Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of variable inside and outside module
Consider the following JavaScript module code. What will be the output when running this code in a modern ES module environment?
Javascript
const secret = 'hidden';
console.log(secret);
{
  const secret = 'inside block';
  console.log(secret);
}
console.log(secret);
A
"hidden"
"inside block"
"hidden"
B
"hidden"
"hidden"
"hidden"
CReferenceError: secret is not defined
D
"inside block"
"inside block"
"inside block"
Attempts:
2 left
💡 Hint
Think about how block scope works with const inside modules.
Predict Output
intermediate
2:00remaining
Effect of top-level await on module scope
What will be the output of this ES module code snippet?
Javascript
const data = await Promise.resolve('loaded');
console.log(data);
ASyntaxError: await is only valid in async functions
B"loaded"
CReferenceError: data is not defined
DPromise { 'loaded' }
Attempts:
2 left
💡 Hint
Modern ES modules support top-level await.
🔧 Debug
advanced
2:00remaining
Why does this module variable cause an error?
This code throws an error when run as an ES module. What is the cause?
Javascript
var count = 0;
function increment() {
  count++;
}
export { increment };
console.log(count);
increment();
console.log(count);
ANo error; output is 0 then 1
BReferenceError because 'count' is not exported
CSyntaxError due to missing export of 'count'
DNo error; output is 0 then 0
Attempts:
2 left
💡 Hint
Variables inside a module are private unless exported, but accessible within the module.
Predict Output
advanced
2:00remaining
Module scope and global scope interaction
What will be the output of this code when run as an ES module?
Javascript
globalThis.value = 10;
const value = 20;
console.log(value);
console.log(globalThis.value);
A
20
20
B
10
20
CReferenceError: value is not defined
D
20
10
Attempts:
2 left
💡 Hint
Module scope variables do not affect globalThis unless explicitly assigned.
🧠 Conceptual
expert
2:00remaining
Why does this module code not pollute global scope?
In ES modules, variables declared at the top level do not become global variables. Why is this behavior important?
AIt makes variables accessible from any script on the page automatically.
BIt forces all variables to be exported explicitly.
CIt prevents accidental overwriting of global variables and keeps modules self-contained.
DIt disables variable declarations outside functions.
Attempts:
2 left
💡 Hint
Think about how large projects avoid conflicts.