1. Introduction

This book is about the popular JavaScript utilities library lodash. Before we discuss lodash, we should understand why we need a JavaScript utilities library. With the prevalence of Web 2.0, Ajax and NodeJS, JavaScript has become a very important programming language in both browser-side and server-side. Besides the bad parts1 of JavaScript language, JavaScript itself doesn’t have a rich set of high-level API for developers to use, which makes common programming tasks hard to complete.

For example, it’s a very common task to iterate an array and process all elements in this array in sequence. In some old browsers, the JavaScript Array object doesn’t have the method forEach(). To iterate an array, for loop is required as in Listing 1.1. process is the function to process elements in the array.

Listing 1.1 Traditional approach to iterate an array
for (var i = 0, n = array.length; i < n; i++) {
  process(array[i]);
}

When using the method forEach(), the code in Listing 1.1 can be simplified as in Listing 1.2.

Listing 1.2 Use forEach() to iterate an array
array.forEach(process);

Comparing code snippets in Listing 1.1 and Listing 1.2, it’s obvious that Listing 1.2 is much simpler to understand and easier to write and maintain than the code in Listing 1.1. That’s why developers want more high-level APIs. JavaScript itself is evolving to add more language features and APIs, but the process is not fast enough. ECMAScript, the specification behind JavaScript, includes nine new methods for searching and manipulating array contents in 5th edition. This means developers can use the method forEach() when the JavaScript engine supports ECMAScript 5. But some old browsers, like IE 8, don’t support ECMAScript 5, which means developers need to consider cross-platform compatibility issues if supporting old browsers is a must. ECMAScript 6 specification was published in June 2015 with a lot of new features and enhancements.

Developers rely on JavaScript libraries to make daily development easier. The goal of libraries is to become the bridge between JavaScript runtime and developers. Developers can enjoy the high-level APIs provided by those libraries. Libraries are responsible for handling implementation details about how to use the low-level JavaScript APIs efficiently.

You may have heard about or even used another JavaScript utilities library Underscore. Underscore provides a rich set of common APIs in the namespace _. Lodash also uses namespace _ and it’s a drop-in replacement of Underscore with more features and performance improvements. If you already use Underscore, you can simply replace the Underscore with lodash, everything should just work.

This book is for the latest lodash 4.17.21 version.

1.1 Installation

Lodash is just a plain old JavaScript library, so it’s very easy to install and use.

1.1.1 Web

In a web application, we can just download the lodash release JavaScript file and include it in the HTML page, then use _ in the JavaScript code.

Listing 1.3 Install lodash in HTML page
<script src="lodash.js"></script>

We can also use links provided by CDN servers to load lodash. CDN servers usually have different versions of lodash to choose from. Listing 1.4 shows how to use cdnjs to load lodash. cdnjs also provides the minified JavaScript version with source mapping file.

Listing 1.4 Load lodash from cdnjs
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.js">
</script>

1.1.2 NodeJS

In NodeJS, we can install lodash using npm or yarn; see Listing 1.5 and Listing 1.6.

Listing 1.5 Install lodash using npm
$ npm install --save lodash

$ npm install --save lodash@4.17.21
Listing 1.6 Install lodash using yarn
$ yarn add lodash

$ yarn add lodash@4.17.21

Then we can use require to import lodash package, see Listing 1.7.

Listing 1.7 Use lodash in NodeJS
var _ = require('lodash'); // Require the whole lodash package

var forEach = require('lodash/forEach'); // Require only forEach

It’s recommended to only install NodeJS modules of actually used modules. For example, if the code only uses _.forEach method, then install the lodash.foreach module only.

Listing 1.8 Use lodash modules
$ npm install --save lodash.foreach

var forEach = require('lodash.foreach');

1.2 Lodash features

Lodash focuses on providing core features that are frequently used for JavaScript built-in objects, including:

  • Arrays
  • Objects
  • Functions
  • Strings

Some of those features may have been included in the latest version of ECMAScript specification. Some platforms may have also implemented extra features. If the underlying platform already supports a certain feature, lodash just uses the native implementation to improve performance.

1.3 Code sample convention

All code samples in this book are written in ECMAScript 6 JavaScript syntax and tested on NodeJS 6.9.4. Most of the code is written as Jest test cases to verify the result. For those code that are not written as Jest code, the result of execution is provided below the actual code as a comment; see Listing 1.9.

Listing 1.9 Code sample convention
_.min([1, 2, 3]);
// -> 1

As in the Listing 1.9 above, _.min([1, 2, 3]); is the actual code, 1 after // -> is the execution result.

The complete source code of this book can be found on GitHub.

1.4 About this book

Lodash is a well-documented JavaScript library with comprehensive official documentation. This book is a simple and concise guide on how to use lodash in practice. It covers core features and most frequently used functions.