Assess your understanding of JavaScript modules, including import/export syntax, module advantages, scope, and related concepts essential in frontend development.
Which statement correctly imports a named export 'calculate' from a file called 'math.js'?
Explanation: The correct syntax for importing a named export is using curly braces. Option B is the default import syntax and only works if 'calculate' is exported as default. Option C is invalid syntax. Option D uses CommonJS syntax, not ES modules.
Which keyword is used to define a default export in a JavaScript module?
Explanation: The 'default' keyword is used with 'export' to designate the default export. The other options are not recognized JavaScript keywords for exporting modules.
How does module-level scope in JavaScript modules differ from script-level scope?
Explanation: Module scope means variables declared in a module are private to that module by default. Option B is incorrect because variables are not global. Option C is false; modules do not automatically inherit globals. Option D wrongly claims only functions are restricted.
What is a main benefit of using JavaScript modules in frontend development?
Explanation: Modules enable better code organization and encapsulation, reducing conflicts. Compression requires separate tools. Load times aren't guaranteed by modules alone. Syntax errors still need fixing by the developer.
Which statement re-exports all exports from 'utils.js' in a new module?
Explanation: 'export * from' syntax re-exports everything. Option B is for default exports and invalid here. Option C is an import statement, not export. Option D uses CommonJS, not ES modules.
How do you import all named exports from 'helpers.js' as a single object called 'helpers'?
Explanation: 'import * as helpers' gathers all named exports under the 'helpers' namespace. Option B imports a default export, not all exports. Option C is invalid syntax. Option D uses CommonJS, which is not ES module syntax.
What happens if you import a module solely for its side effects, without importing any bindings?
Explanation: Importing a module without bindings still executes its top-level code. Option B is false; the code does run. Option C is incorrect; imports can be just for side effects. Option D is wrong; this is valid syntax in ES modules.
When importing a local module in a browser ES module environment, what file extension is required?
Explanation: Browsers generally require the '.js' extension for module imports. '.module' is not recognized. '.mjs' is used in Node.js but less common in browsers. Omitting the extension usually results in an error.
Which statement is true of exporting multiple functions from a JavaScript module?
Explanation: JavaScript modules support multiple named exports, making them flexible. The other options are incorrect: there is no one-export or one-function-per-module limitation, and functions do not have to be default exports.
How do you dynamically import a module in JavaScript at runtime?
Explanation: The dynamic import syntax uses 'import()' as a function that returns a promise. 'require' is not valid in browser ES modules. The third option uses static import syntax, and the fourth is an export statement.