Skip to content

What is a library?

Generally speaking, JavaScript libraries are collections of prewritten code snippets that can be used (and reused) to perform common JavaScript functions. JavaScript library code can be plugged into the rest of your project’s code on an “as needed” basis.

Wikipedia

“In computer science, a library is a collection of non-volatile resources used by computer programs, often for software development. These may include configuration data, documentation, help data, message templates, pre-written code and subroutines, classes, values or type specifications.”

JavaScript frameworks and libraries give developers the ability to use prewritten code for common JavaScript functions, and to create their own functions that can then be reused as needed. So does that mean “library” and “framework” two ways of saying the same thing? Not really. Yes, both tools have similar uses, but there are significant differences between the scope and scale of the two platforms.

development puzzle pieces

As programmers, we should re-use existing code when we can, so that we don't waste our time writing code that another programmer has already written.

In JavaScript, the way we do that is by using a library. A library is a JavaScript file that contains a bunch of functions, and those functions accomplish some useful task for your webpage.

How do we know what functions we can use? We could look at the JavaScript file, if it's short, or better, we could look at the documentation. Most libraries have documentation with a list of available functions or a real-world example.

Example of usage

In traditional JavaScript

var messages = document.getElementsByClassname('message');
messages.forEach(function(message) {
    message.className = message.className + " visible";
});

With jQuery (library)

$('.message').addClass('visible');