Module in JavaScript
Introduction
- Is a reusable piece of code
- Modules are building blocks of any complicated Software
- Module encapsulates implementation details
- Modules can be imported
- Modules can be exported
- Modules are imported synchronously
Example
- Create file index.html
- Create a file script.js
- Create a file auth.js
//index.html <html> <body> <script type="module" defer src="./script.js"></script> </body> </html> //script.js import { checkAuth } from './auth.js' let isValidUser = checkAuth(123); console.log(isValidUser); //auth.js export const checkAuth = (userId) => { let validUser = true; return validUser; }