What does Double Underscore (__) in Front of a Variable in Node.js ?
Last Updated :
18 Jun, 2024
In the world of Node.js, variable naming conventions play a crucial role in code readability, maintainability, and the prevention of naming conflicts. One particular convention that often intrigues developers, especially those new to Node.js, is the use of a double underscore (__
) in front of a variable. This article delves into the significance and usage of double underscores in Node.js, providing insights and examples to help you understand this convention better.
What Does Double Underscore (__
) Mean in Node.js?
Double underscore (__
) in Node.js is a naming convention typically used to signify certain characteristics of variables or properties. While it doesn't have an inherent syntactical meaning in JavaScript or Node.js, it carries specific conventions and practices in the community, particularly in relation to module and scope management.
There were only two variables in (called global objects) with double underscores in Node. js.
- __dirname: The __dirname in a node script returns the path of the folder where the current JavaScript file resides.
- __filename: The __filename in Node.js returns the filename of the code that is executed. It gives the absolute path of the code filename.
Using Underscore(_) - Private Variable
Example: Below is an example of a private variable.
JavaScript
(function () {
// Define two variable
const _b = 456;
const _a = 123;
console.log("a =", _a); // => 123
console.log("b =", _b);
})();
Using Double underscore (__) - Global Variable.
The __dirname is an environment variable that tells you the absolute path of the directory containing the currently executing file.
Syntax:
console.log(__dirname)
Return Value:
It returns the absolute directory name of the current module.
Example 1: Create a JavaScript file app.js and write down the following code.
JavaScript
// Node.js code to demonstrate the absolute
// file name of the current Module.
console.log("Directory Name of the current file is: ",
__dirname);
Output:
C:\Users\Pallavi\Desktop\NODEJS PROJECTS\NodeJS-Projects\Express_Session
__filename
The __filename in Node.js returns the filename of the code that is executed. It gives the absolute path of the code file. The following approach covers how to implement __filename in the NodeJS project.
Syntax:
console.log(__filename)
Return Value:
It returns the absolute filename of the current module.
Example 2: Create a JavaScript file app.js and write down the following code.
JavaScript
// Node.js code to demonstrate the absolute
// file name of the current Module.
console.log("Filename of the current file is: ",
__filename);
Output:
C:\Users\Pallavi\Desktop\NODEJS PROJECTS\NodeJS-Projects\Express_Session\app.js
Conclusion
The double underscore (__
) prefix is a useful convention in Node.js for managing variable scope, avoiding naming conflicts, and indicating internal or special-purpose variables. While it doesn’t have intrinsic syntactical meaning, its significance lies in the clarity and structure it brings to your code. By understanding and following the best practices outlined in this article, you can leverage double underscores effectively in your Node.js projects, leading to more maintainable and conflict-free codebases.
Reference: https://nodejs.org/api/globals.html#globals_filename
Similar Reads
What is the Purpose of __filename Variable in Node.js ? In Node.js, there are several global variables that are available in all modules. One such variable is __filename. This article delves into the purpose, usage, and practical applications of the __filename variable in Node.js.What is __filename?The __filename variable is a built-in global variable in
3 min read
What is the use of underscore variable in REPL ? In the previous Node.js REPL post, we've discussed what is Node.js REPL and how to use it on your command prompt. We've also discussed how to perform arithmetical operations, how to use node library functions, and how to use loops in REPL. In this article, we're going to discuss what is the use of t
2 min read
Single and Double Underscores in Python In Python, naming conventions play a crucial role in code readability and maintainability. Single and double underscores, when used in names, convey specific meanings and conventions. These naming conventions are widely adopted in the Python community and are often utilized in various contexts, incl
3 min read
Underscore.js _.value() Function Underscore.js is a JavaScript library that makes operations on arrays, string, objects much easier and handy. The _.value() function is used to extract the value of the wrapped object. This function is mainly used with chain function in JavaScript. Note: It is very necessary to link the underscore C
2 min read
Underscore.js _.values() Function The _.values() function is used to return the list of all values of the object element. Syntax: _.values( object ) Parameters: This function accepts one parameter as mentioned above and described below: object: It contains the object element that holds the elements of key and value pair. Return Valu
1 min read