Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does _(variable_name) mean in javascript?

Tags:

I came across the following javascript code:

this.removeEdge = function(source, target) {   if(!_states[source]) return;    var children = _states[source].children,       index = _(children).indexOf(target);   if(index !== -1) children.splice(index, 1); }; 

What does _(children) mean?

like image 714
Charles Gao Avatar asked Apr 16 '13 20:04

Charles Gao


People also ask

What does _variable mean in JavaScript?

Variable means anything that can vary. In JavaScript, a variable stores the data value that can be changed later on. Use the reserved keyword var to declare a variable in JavaScript.

What does '$' mean in JavaScript?

Updated on July 03, 2019. 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.

What does _ in front of variable mean?

A single leading underscore in front of a variable, a function, or a method name means that these objects are used internally. This is more of a syntax hint to the programmer and is not enforced by the Python interpreter which means that these objects can still be accessed in one way on another from another script.

What does double underscore mean in JS?

Double underscore (__) in front of a variable is a convention. It is used for global variable (The following variables may appear to be global but are not, rather local to each module) in Nodejs meanwhile Underscore(_) used to define private variable.


1 Answers

_ is a valid variable identifier in JavaScript, and could theoretically refer to anything. Using _(...) with function syntax implies that _ is a function.

That said, it is commonly used by the underscore.js library, however if you're looking at minified code, it's quite possibly being used as another single-character variable name to save on file size.


In your example provided, it appears that underscore.js is being used to treat children as a collection, so that the indexOf function can be applied to the collection. This would be similar to calling:

_.indexOf(children, target); 
like image 94
zzzzBov Avatar answered Oct 24 '22 08:10

zzzzBov