JavaScript Modules 2 — Questions and Answers
Question 1: What does `export default` allow you to do differently than named exports?
- Export multiple values under one name
- Export a single value without specifying a name on import (Correct answer)
- Prevent re-exporting from other modules
- Export only functions, not variables
Correct answer: Export a single value without specifying a name on import
`export default` lets the importing module choose any name for the imported value, since there's only one default per module.
Question 2: Which syntax correctly re-exports a named export `helper` from `./utils.js`?
- import { helper } from './utils.js'; export helper;
- export { helper } from './utils.js'; (Correct answer)
- export default helper from './utils.js';
- re-export { helper } from './utils.js';
Correct answer: export { helper } from './utils.js';
The `export { name } from 'module'` syntax re-exports without importing into the current scope.
Question 3: What happens if you use `import` at the top of a script tag without `type="module"`?
- It works but only for relative paths
- The browser treats it as CommonJS
- A SyntaxError is thrown because import is only valid in module context (Correct answer)
- The import is silently ignored
Correct answer: A SyntaxError is thrown because import is only valid in module context
The `import` declaration is only valid inside ES modules; using it in a classic script causes a SyntaxError.
Question 4: What is the value of `import.meta.url` inside an ES module?
- The URL of the entry-point module
- The URL of the current module file (Correct answer)
- undefined
- The base URL of the server
Correct answer: The URL of the current module file
`import.meta.url` resolves to the absolute URL of the specific module file in which it appears.
Question 5: How can you import all named exports from a module into a single namespace object?
- import * as ns from './mod.js'; (Correct answer)
- import { * } from './mod.js';
- import ns = require('./mod.js');
- import all from './mod.js';
Correct answer: import * as ns from './mod.js';
The `import * as ns` syntax creates a module namespace object containing all named exports.
Question 6: Which statement about circular dependencies in ES modules is true?
- They always throw a ReferenceError at runtime
- They are forbidden by the spec
- Live bindings allow circular imports to work if values are initialized before use (Correct answer)
- Circular imports are silently converted to undefined
Correct answer: Live bindings allow circular imports to work if values are initialized before use
ES module live bindings mean circular dependencies can work as long as the binding is populated before it is actually read.
Question 7: What does the `as` keyword do in `export { foo as bar }`?
- Creates an alias so importers use `bar` instead of `foo` (Correct answer)
- Renames `foo` inside the current module
- Makes `bar` the default export
- Prevents `foo` from being used locally after export
Correct answer: Creates an alias so importers use `bar` instead of `foo`
The `as` keyword in an export renames the binding so consumers import it under the new name `bar`.
What does `export default` allow you to do differently than named exports?