Open In App

What does Double Underscore (__) in Front of a Variable in Node.js ?

Last Updated : 18 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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);
})();

Output
a = 123
b = 456

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



Next Article

Similar Reads