Modules are essential concepts in JavaScript. the variables and functions are grouped together so that things are organized.
They are highly self-contained and grouped together with their own specific functionality allowing them to be moved around used by other places or even removed without disrupting the whole system.
Why do we need module?
var name = "Mike";
<html>
<script>
var name = "Tim";
var value = "?";
</script>
<script src="./lib.js"></script>
<script>
if (name === "Tim") {
console.log("The name is: " + name);
}
if (name === "Mike") {
console.log("The name is: " + name);
}
</script>
</html>
The name is Mike
- The variable name is overwritten. This is a PROBLEM
As you can see, without a module it is really hard to debug, and as your code is getting bigger and bigger with your JavaScript. It becomes almost impossible to maintain. Thus, what is a module? a module is just a file. one script is one module that may contain a class or a library of functions for a specific purpose.