Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the Underscore module with Node.js

People also ask

What is underscore NPM?

Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects.

Why do we use _ in JavaScript?

The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.

Should I use underscore JavaScript?

the Underscore Prefix in JavaScript VariablesThe underscore _ is simply an acceptable character for variable/function names; it has no additional functionality. Underscore variable is the standard for private variables and methods. There is no true class privacy in JavaScript.


As of today (April 30, 2012) you can use Underscore as usual on your Node.js code. Previous comments are right pointing that REPL interface (Node's command line mode) uses the "_" to hold the last result BUT on you are free to use it on your code files and it will work without a problem by doing the standard:

var _ = require('underscore');

The Node REPL uses the underscore variable to hold the result of the last operation, so it conflicts with the Underscore library's use of the same variable. Try something like this:

Admin-MacBook-Pro:test admin$ node
> _und = require("./underscore-min")
{ [Function]
  _: [Circular],
  VERSION: '1.1.4',
  forEach: [Function],
  each: [Function],
  map: [Function],
  inject: [Function],
  (...more functions...)
  templateSettings: { evaluate: /<%([\s\S]+?)%>/g, interpolate: /<%=([\s\S]+?)%>/g },
  template: [Function] }
> _und.max([1,2,3])
3
> _und.max([4,5,6])
6

Or :

    var _ = require('underscore')._;

The name _ used by the node.js REPL to hold the previous input. Choose another name.