Skip to main content

JavaScript modules:

module

  1. What are JavaScript modules?

Modules

  • encapsulate code
  • control access
  • reference its own dependencies internally
  • extract hardcoded constants to get rid of unnecessary duplication which can lead to bugs.

import

export

rename

export { getSessions as sessions } 


import sessions from ....

default: to simplify exports, without having to expose variables

export default function getSessions() {
}

OR

export { getSessions as default, sessionUrl };
  • when compared to the constructor design pattern, generally there is only one of the module object like a service.
  1. What are the benefits of using JavaScript modules?
  2. How do you create a JavaScript module?
  3. What is the difference between named and default exports in a JavaScript module?
  4. How do you import a JavaScript module in another file?
  5. What is the difference between a relative and an absolute path when importing a JavaScript module?
  6. How do you use a JavaScript module in an HTML file?
  7. What are the different types of JavaScript module formats?
  8. What is the purpose of the "use strict" directive in a JavaScript module?
  9. What is the ES6 module loader API and how does it work?