Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
What is the Purpose of __filename Variable in Node.js ?
Next article icon

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.

Table of Content

  • What Does Double Underscore (__) Mean in Node.js?
  • Using Underscore(_) - Private Variable
  • Using Double underscore (__) - Global Variable.
  • __filename
  • Conclusion

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
What is the Purpose of __filename Variable in Node.js ?
author
_sh_pallavi
Improve
Article Tags :
  • Web Technologies
  • Node.js
  • Node.js-Methods
  • NodeJS-Questions

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 i
    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
  • What are the Underscore Methods in Backbone.js ?
    Backbone.js is a JavaScript framework that provides the structure of web applications by separating the application's data (models) from its presentation (views) and its logic (controllers). It is designed for minimal and flexible, and it works well with other libraries and frameworks, such as Under
    6 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
    4 min read
  • What are the variable naming conventions in JavaScript ?
    When we name variables in javascript, we've to follow certain rules. Each variable should have a name that appropriately identifies it. Your JavaScript code gets easier to comprehend and work with when you use suitable variable names. It's critical to name variables correctly. For example Constants
    2 min read
  • What are All the Uses of an Underscore in Scala?
    The underscore (_) is a symbol frequently employed in Scala, serving as a handy tool to simplify and condense code. While it's dubbed "syntactic sugar" for its ability to streamline code, its extensive use can sometimes lead to confusion and make the learning process more challenging. This article f
    9 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
  • How to create a variable using a user-defined name in JavaScript ?
    In this article, we will learn to create a variable using the user-defined name in JavaScript. Variables in JavaScript: Basically, it is a container that holds reusable data in JavaScript. The variable's value can be changed during program execution and should be declared before using it. Before ES6
    2 min read
  • How to pass a Node.js variable to the inside of a Pug script tag ?
    In Node.js with Pug, we can pass the variables to the Pug template engine using the res.render method. To make the variable available inside the Pug script tag, we include it in the object passed to res.render and then reference it within the Pug file using the syntax "#{variableName}" inside the sc
    3 min read
  • What is the purpose of the 'node_modules' folder ?
    The node_modules folder is a directory in NodeJS projects that stores third-party libraries and dependencies. It's essential for managing dependencies, which are packages or modules that a NodeJS project relies on. When you install a package using npm or Yarn, these tools download the package along
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences