Using Libraries in JavaScript

Let's look at the general principle of using libraries. Suppose we needed a library with necessary functions for working with arrays. Suppose we've googled the lodash library.

We go to the library website and look for links to download the library. As a rule, libraries are provided in two versions: regular and minified. The regular one is useful if you want to examine the source code of a library. The minimized version is convenient because it takes up less space.

So, we are looking for a download link and downloading the library. Sometimes it happens that the documentation provides the link to a JavaScript file. In this case, this file, instead of being downloaded, will simply open in the browser. In this case, you need to select save in the browser menu or press Ctrl + S and save the library as a file.

The downloaded library must be connected to your HTML file:

<!DOCTYPE html> <html> <head> <title></title> <script src="lodash.js"></script> </head> <body> </body> </html>

The connected library creates one or more variables in a global scope. In the case of lodash, this variable is called _. Let's now connect our file below the library connection, in which we will write the code of our site:

<!DOCTYPE html> <html> <head> <title></title> <script src="lodash.js"></script> <script src="test.js"></script> </head> <body> </body> </html>

In this file, a global variable will be available, which is created by the previously included lodash library:

console.log(_); // there is lodash library in this variable

Let's use one of the included library methods:

let res = _.chunk(['a', 'b', 'c', 'd'], 2); console.log([['a', 'b'], ['c', 'd']]);

Download the library underscorejs. Connect it to your HTML file. Try out some functions from this library.