Javascript Modules

In this blog we are gonna learn about js module .
What is a js module?
A js modules is a javascript file which has its own scope and came be import and export code.
Module helps us to reuse and organize the code .
How to export from a module?
//File name user.js
const name="karan"
export {name};
How to import from a module?
suppose we have a file user.js and we have a variable name in that file and I want acces that variable in a different file , so first we export that variable and then import in our module or file.
Syntax:
import variable name from path
import {name} from "./user.js"
console.log(name)
//Output
karan
We can also export or import multiple functions and variables from a module.
import {email,password,loginUser} from ".user.js"
const res=loginUser(email,password)
default vs named exports:
There are multiple ways to export values
Default export
const userName="Karan";
export default userName;
import userName from "./user.js"
console.log(userName)
//output
Karan
You can only export one entity using default export , default export means import with any name but using normal export we can import with the same name and multiple entities.
Conclusion:
JavaScript modules help you write clean, organized, and reusable code by splitting it into separate files. Using export and import, you can easily share data and functions between files. Named exports allow multiple values, while default export is used for a single main value. Overall, modules make your code more maintainable and scalable.


