2

What does _ mean in this example code:

if (_(abc.content).has("abc")){
    console.log("abc found");
}

Many people say "_" means a private member, but if abc or content is a private member, shouldn't we use _abc.content or abc._content?

Thank you

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
DBSQUARED
  • 73
  • 6
  • it has no "special" meaning at all in javascript ... it's just the same as `a` – Jaromanda X Nov 26 '18 at 00:48
  • 1
    In the given example `_` is a function like `$` but its mostly found with the popular util libraries such as lodash or underscore. – dotconnor Nov 26 '18 at 00:48
  • **Probably dupe:** [What does _(variable_name) mean in javascript?](https://stackoverflow.com/questions/16046532/what-does-variable-name-mean-in-javascript) – Ele Nov 26 '18 at 01:02

3 Answers3

3

For that to be valid, _ must refer to a function. Perhaps the script is using underscore, in which case _(abc.content).has("abc") returns a Boolean - true if the abc.content object has a key of abc, and false otherwise:

const abc = { content: { key1: 'foo', abc: 'bar' } };

if (_(abc.content).has("abc")){
    console.log("abc found");
}

console.log(_(abc.content).has("keyThatDoesNotExist"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>

It probably has nothing to do with private properties, because _ is a standalone function.

The library used might also be lodash:

const abc = { content: { key1: 'foo', abc: 'bar' } };

if (_(abc.content).has("abc")){
    console.log("abc found");
}

console.log(_(abc.content).has("keyThatDoesNotExist"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.min.js"></script>

But to be sure, you'll have to examine _ - console.log it, or see where it's defined, to get some idea.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

That's just a variable name. You are right, conventions suggest that underscore refer to private members in an object such as:

const num = 2;

function Multiply(num) {
    this._multiplier = 2;
    this._input = num;

    this.start = function(){
        return this._multiplier * this._input;
    }
}

const product = new Multiply(num).start(); //4

But the concept of private members has nothing to do with your example.

In your case, _() is actually a function;

function _ (){
    return "I love potatoes";
}

a function that returns an object that contains the .has() method. The structure of that function of yours could be dumbed down to something like

function _(args){
    const content = args;

    return {
       has: function(data){
          //do something
          return true; //some boolean expression
       }
    }
}
Abana Clara
  • 4,602
  • 3
  • 18
  • 31
0

_ is a valid variable name in JavaScript and can be used in any way you want just like i, a, x, length, time, date or any other variable name you can come up with.

Personally I usually use underscore as a variable in a function parameter list when I wont be using that variable in the function body.

const func = (_) => {
  // _ is not used in the function body
};

const _ = () => { console.log('Hello from underscore!'); };

_();
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60