0
0
Javascriptprogramming~20 mins

Why modules are used in Javascript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Module Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use modules in JavaScript?

Why do developers use modules in JavaScript?

ATo convert JavaScript into another language automatically
BTo organize code into reusable pieces and avoid name conflicts
CTo prevent the code from running in browsers
DTo make the code run faster by skipping parsing
Attempts:
2 left
💡 Hint

Think about how breaking code into parts helps manage big projects.

Predict Output
intermediate
2:00remaining
Output of importing a module

What will be the output of this JavaScript code using modules?

Javascript
export const greeting = 'Hello';

import { greeting } from './module.js';
console.log(greeting);
AReferenceError
Bundefined
CSyntaxError
DHello
Attempts:
2 left
💡 Hint

Imported variables keep their exported values.

Predict Output
advanced
2:00remaining
What happens if you import a module twice?

Consider this code:

import { value } from './mod.js';
import { value as val2 } from './mod.js';
console.log(value, val2);

What is the output?

A42 42
B42 undefined
CReferenceError
DSyntaxError
Attempts:
2 left
💡 Hint

Modules are cached and importing multiple times returns the same values.

🧠 Conceptual
advanced
2:00remaining
Why do modules help avoid global scope pollution?

How do JavaScript modules help prevent global scope pollution?

ABy running code only once per browser session
BBy automatically renaming all variables globally
CBy keeping variables and functions inside the module scope instead of the global scope
DBy disabling global variables entirely
Attempts:
2 left
💡 Hint

Think about where variables live when you use modules.

Predict Output
expert
2:00remaining
Output of circular module imports

Given two modules:

// a.js
import { bValue } from './b.js';
export const aValue = 'A';
console.log('a.js:', bValue);

// b.js
import { aValue } from './a.js';
export const bValue = 'B';
console.log('b.js:', aValue);

What will be printed when a.js is run?

A
b.js: undefined
a.js: B
B
b.js: A
a.js: B
CReferenceError due to circular import
D
b.js: undefined
a.js: undefined
Attempts:
2 left
💡 Hint

Think about how JavaScript handles circular imports and initialization order.