Lodash – Functional Javascript library



If you are familiar with functional programming language such as Haskell, Erlang… or fell in love with awesome collection method of Scala, you will may need to develop your front end application with these features too.
Luckily, we have a modern JavaScript utility library delivering modularity, performance, & extras, it named as Lodash.

Lodash is inspired by Underscore, but nowadays is superior solution and Lodash is updated more frequently than Underscore.

Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc.



  • Download  source
  • Via npm
     $ npm i --save lodash




function square(n) {
  return n * n;
_.map([1, 2, 4, 8], square);
// → [1, 4, 16, 64]
_.map({'a': 1, 'b': 2, 'c': 4, 'd': 8 }, square);
// → [1, 4, 16, 64] (iteration order is not guaranteed)

var users = [
 {username: 'LongTV', age: 28},
 {username: 'ThuNX', age: 24},
 {username: 'DuyBQ', age: 33}
// The `_.property` iteratee shorthand.
_.map(users, 'username');
// → ['LongTV', 'ThuNX', 'DuyBQ']

function info(user) {
 return [user.username, user.age];
_.flatMap(users, info)
// → ['LongTV', 28, 'ThuNX', 24, 'DuyBQ', 33]

filter – order

var users = [
 {username: 'LongTV', age: 28, active: true},
 {username: 'ThuNX', age: 24}, active: false},
 {username: 'DuyBQ', age: 33}, active: true},
// Sort by `user` in ascending order and by `age` in descending order.
_.orderBy(users, ['user', 'age'], ['asc', 'desc']);
// → [{username: 'DuyBQ', age: 33, active: true}, {username: 'LongTV', age: 28, active: false}, {username: 'ThuNX', age: 24, active: false}];

_.filter(users, function(o) { return !o.active; });
// → objects for ['LongTV', 'ThuNX']
// Shorthand.
_.filter(users, { 'age': 33, 'active': true });
// → objects for ['DuyBQ']
_.filter(users, ['active', false]);
// → objects for ['LongTV', 'ThuNX']
_.filter(users, 'active');
// → objects for ['DuyBQ']

There are many collection function such as:

  • _.every //Returns true if all elements pass the predicate check, else false.
  • _.find //Returns the matched element, else undefined.
  • _.groupBy //Returns the composed aggregate object.
  • _.includes //Returns true if value is found, else false.
  • _.sortBy //Returns the new sorted array.
  • _.partition //Returns the array of grouped elements.
  • _.reduce //Returns the accumulated value.
  • _.reject //Returns the new filtered array.

To figure out more method, you can go to document in this page


It’s also contains function interact with string

  • _.lowerCase //Returns the lower cased string
  • _.repeat //Returns the repeated string.
  • _.truncate // Returns the truncated string.
  • _.upperFirst //Returns the converted string with first letter upcase.
  • _.template //Returns the compiled template function.
// Use the "interpolate" delimiter to create a compiled template.
var compiled = _.template('hello <%= user %>!');
compiled({ 'user': 'fred' });
// → 'hello fred!'

// Use the "evaluate" delimiter to execute JavaScript and generate HTML.
var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
compiled({ 'users': ['fred', 'barney'] });
// → '<li>fred</li><li>barney</li>'


Common function deal with value of variable

  • _.isDate
  • _.isElement
  • _.isEmpty
  • _.isEqual
  • _.isFunction
  • _.isInteger
  • _.isLength
  • _.isMatch
  • _.isNaN
  • _.isNull
  • _.isNumber

It’s very helpful and save a lot of time while develop application. You just need to focus on the main problem and doesn’t care about issue of Javascript, for example like: NaN is not equal NaN  or truthy and falsy things.


Lodash has been doing great for me and I will continue to drop it in all my Javascript project. It reduces the amount of boilerplate code and also improves the clarity of my application logic.

But my biggest takeaway is this – Lodash forces me to think in a more functional manner. I can break my application into many smaller modules with singular focus and no side effects.

There’s no reason you shouldn’t try it and figure out yourself what it’s interesting.

Thanks for your reading.

Add a Comment

Scroll Up