For sure.
Modules are important when programs get large and/or are built by multiple people.
They are an important part of a divide and conquer strategy by making it easier to truly divide the code into separate parts. A name in one space is different from the same name in other spaces.
The order receiving, order fulfillment, order acknowledgement, and order billing modules might all have a global variable calledcurrentOrder
, and those would be separate and distinct names.They need to have clear interfaces as well, like stereo components and music players.
Yes, either is fine. The reading should have
said var
, let
, or const
,
not just var
.
There are subtle distinctions between var
and let
that rarely matter, so they are almost always
interchangeable.
let
is newer and shinier, so feel free to prefer that
if you want.
const
was introduced at the same time
as let
, and it has some advantages at times.
See strict mode
Sure. Because the separation of modules is so strict in JavaScript,
it can sometimes get in the way. globalThis
is a "super"
variable that exists in all namespaces. So module A can
assign to it and module B can see that value.
I find it useful for debugging, where module A can put a value
into globalThis
and I can see that value in the debugger.