Challenge - 5 Problems
ES Modules Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
What is the output of this ES module import code?
Consider two files:
In
What will
mathUtils.mjs and app.mjs.mathUtils.mjs exports a named function square.In
app.mjs, we import square and call it with 4.What will
console.log(result) print?Node.js
/* mathUtils.mjs */ export function square(x) { return x * x; } /* app.mjs */ import { square } from './mathUtils.mjs'; const result = square(4); console.log(result);
Attempts:
2 left
💡 Hint
Remember that named exports must be imported with curly braces matching the exported name.
✗ Incorrect
The function square is exported as a named export and imported correctly. Calling square(4) returns 16, which is logged.
❓ component_behavior
intermediate2:00remaining
What happens when importing a default export incorrectly?
Given
Which import statement in
utils.mjs exports a default function:export default function greet() { return 'Hello'; }Which import statement in
app.mjs will cause a runtime error when calling greet()?Attempts:
2 left
💡 Hint
Default exports are imported without curly braces.
✗ Incorrect
Option A tries to import a named export 'greet' which does not exist, causing greet to be undefined and calling greet() throws a TypeError.
🔧 Debug
advanced2:00remaining
Why does this ES module import cause a SyntaxError?
Look at this code snippet in
Running this code causes a SyntaxError. What is the most likely cause?
app.mjs:import { add } from './math.mjs'Running this code causes a SyntaxError. What is the most likely cause?
Node.js
import { add } from './math.mjs';
Attempts:
2 left
💡 Hint
Check Node.js version and module support.
✗ Incorrect
Node.js versions before 13 require the --experimental-modules flag to run ES modules. Without it, import statements cause SyntaxError.
❓ state_output
advanced2:00remaining
What is the value of variable 'count' after these imports and mutations?
In
In
What will be logged?
counter.mjs we have:export let count = 0;
export function increment() { count++; }In
app.mjs:import { count, increment } from './counter.mjs';
increment();
console.log(count);What will be logged?
Attempts:
2 left
💡 Hint
ES module imports are live bindings: mutations to the exported variable from within the module are visible to importers.
✗ Incorrect
Imports in ES modules are live bindings to the exports. The increment() function mutates the shared 'count' variable, so the imported binding sees the updated value of 1.
🧠 Conceptual
expert3:00remaining
Which option correctly re-exports a named export with a new name?
You want to re-export a named export
Which code in
foo from moduleA.mjs as bar in moduleB.mjs.Which code in
moduleB.mjs achieves this?Attempts:
2 left
💡 Hint
Use export with alias syntax.
✗ Incorrect
Option C correctly re-exports 'foo' as 'bar'. Option C imports with alias but exports original name 'foo' which is undefined here. Option C exports all as 'bar' namespace, not a renamed export. Option C tries to export 'bar' which does not exist in moduleA.