Learn Simpli

Free Online Tutorial For Programmers, Contains a Solution For Question in Programming. Quizzes and Practice / Company / Test interview Questions.

Module in JavaScript

Introduction
  1. Is a reusable piece of code
  2. Modules are building blocks of any complicated Software
  3. Module encapsulates implementation details
  4. Modules can be imported
  5. Modules can be exported
  6. Modules are imported synchronously
Example
  1. Create file index.html
  2. Create a file script.js
  3. 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;
}