Practice JavaScript Modules and Imports/Exports with Solutions
JavaScript Modules and Imports/Exports [20 exercises with solutions]
Default Export:
1. Write a JavaScript module that exports a default function to greet a user and use it in another file.
Named Exports:
2. Write a JavaScript module that exports multiple named functions for basic arithmetic operations.
Import all as Alias:
3. Write a JavaScript programme that imports all exports from a module as a single object and use its properties.
Combining Default and Named Exports:
4. Write a JavaScript module with both default and named exports. Use both in another file.
Re-exporting:
5. Write a JavaScript module that re-exports functions from another module.
Import with Alias:
6. Write a JavaScript programme that imports named exports from a module using alias names and use them in another file.
Dynamic Imports:
7. Write a JavaScript programme that uses dynamic imports to load a module and invoke its function asynchronously.
Conditional Imports:
8. Write a JavaScript program that imports a specific module conditionally based on a runtime value.
Exporting Constants:
9. Write a JavaScript program to create a module that exports multiple constants. Use them in another file.
Aggregating Exports:
10. Write a JavaScript program to aggregate exports from two different modules into one module and use them.
Importing JSON Data:
11. Write a JavaScript program to import a JSON file as a module and access its properties.
Exporting Classes:
12. Write a JavaScript program to create a module that exports a class. Instantiate and use the class in another file.
Tree Shaking:
13. Write a JavaScript program to create a module with multiple exports and import only the functions needed.
Default Import Alias:
14. Write a JavaScript program to import a default export using a custom name and invoke it.
Namespace Import:
15. Write a JavaScript program that imports an entire module as a namespace and access its properties.
Circular Dependency:
16. Write a JavaScript program to create two modules that import each other and handle the circular dependency.
Exporting Functions Inline:
17. Write a JavaScript program to export a function directly during its definition and import it.
Importing Without Using:
18. Write a JavaScript program to import a module for its side effects without directly using any exports.
Overriding Exports:
19. Write a JavaScript program to create a module and override its exports dynamically in another file.
Mixing ES6 and CommonJS Modules:
20. Write a JavaScript program that uses an ES6 module to import a CommonJS module and vice versa.
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics