What's the difference between modules and libraries?

https://www.codecademy.com/paths/web-development/tracks/webdev-intermediate-javascript/modules/intermediate-javascript-modules/lessons/modules/exercises/hello-modules

With reference to the description given in the Web Development modules topic, in the introduction it describes modules as being able to : " fix, and debug code more easily;

  • reuse and recycle defined logic in different parts of our application;
  • keep information private and protected from other modules;
  • and, importantly, prevent pollution of the global namespace and potential naming collisions, by cautiously selecting variables and behavior we load into a program."

This sounds similar to libraries, so how does modules differ from libraries?

I’m going to go right out on a limb and suggest a basic difference between modules and libraries…

modules are object literals with attributes and methods

libraries are function objects with methods

Not much of a distinction when we come down to it. Object literals cannot be used as constructors. Functions, that is ALL functions can be used as constructors.

Take for example jQuery. It IS a function, in and of itself. Each time invoked, it constructs another custom object and through its prototype gives those objects all its built in methods.

Modules don’t do that, as near as I can tell. This of course needs verification.

https://stackoverflow.com/questions/31339656/difference-between-a-module-and-library-in-javascript

It pays to search, and verify or read up on anything that ever comes into question. Make it second nature and watch your knowledge base grow. As always, expect off-the-cuff answers that may contain some semblance of truth. They are a gold mine for search terms, if nothing else.

Thank you for your reply and your wise advice at the end!

1 Like