6

I have encountered 2 conflicting naming conventions while using nodeJS. Some variables start with a single underscore, like _temp, while some start with double underscore, like __dirname.

I have been trying to search for an answer to this naming convention difference. I checked out Double Underscore in front of a variable but the answer was very specific to __dirname and __filename. It didn't address the conflicting naming convention.

It will be great if someone could provide me with a resource for this.

Community
  • 1
  • 1
chaudharyp
  • 3,394
  • 3
  • 26
  • 42

2 Answers2

8

Even I am new to node.js and searching for the answer. Found the answer in node.js documentation. Refer Node.js - Globals for more details.

As mentioned above answer, variable with single underscore (_private) is to define private variable.

Double underscore (__) is not of any convention in node.js. There were only two variables (called global objects) with double underscores in node.js.

__dirname : used when to get the name of the directory that the currently executing script resides in.

__filename : used to get the filename of the code being executed.

SuRa
  • 503
  • 2
  • 12
  • 24
7

I can be wrong, but as far as I know, there is only one convention in js: "if method or variable supposed to be private, use underscore in front of it - _privateMethod". And even this one is kind of "unofficial". Double underscore is not a naming convention. Just some developer from node decided to name thing like this.

Sergey Yarotskiy
  • 4,536
  • 2
  • 19
  • 27
  • I suspect, there was a thought that single underscore would be reserved for a developer to add private properties so as a framework, Express decided they would use double underscore to avoid conflicting with any single underscore properties their developers might be using. But, as you say there is no "official" guide for this. – jfriend00 Mar 11 '16 at 19:30