Why Use a Library
Course- Javascript >
Libraries are reusable collections of JavaScript code that let you do complicated things by adding only a few lines of extra code to your program.
There are many freely available JavaScript libraries that can help you quickly develop capable, cross-browser applications.
Several of the more popular libraries are introduced in this section.
Why Use a Library?
You’ll often see opinions expressed, mainly on the Internet, by JavaScript developers who strongly advocate writing your own code instead of using one of the many available libraries. Popular objections include
- You won’t ever really know how the code works because you’re simply employing someone else’s algorithms and functions.
- JavaScript libraries contain a lot of code you’ll never use but that your users have to download anyway.
Like many aspects of software development, these are matters of opinion. Personally, I believe that there are some very good reasons for using libraries sometimes:
- Why invent code that somebody else has already written? Popular JavaScript libraries tend to contain the sorts of abstractions that programmers need often— which means you’ll likely need those functions too from time to time. The thousands of downloads and pages of online comment generated by the most-used libraries pretty much guarantee that the code they contain will be more thoroughly tested and debugged than your own, home-cooked code would be.
- Take inspiration from other coders. There are some really clever programmers out there; take their work and use it to improve your own.
- Using a well-written library can really take away some of the headaches of writing cross-browser JavaScript. You won’t have every browser always at your disposal, but the library writers—and their communities of users—test on every leading browser.
- Download size for most libraries is not horrific. For the few occasions where you need the shortest of download times, compressed versions are available for most of the popular libraries that you can use in your “production” websites. There’s also the possibility of examining the library code and extracting just the parts you need.