Q1. What are global objects in Node.js?
Global objects are available in all modules without requiring them. Important globals include: __dirname (directory path of current module), __filename (file path), process (process information), console (logging), Buffer (binary data), setTimeout/clearTimeout, setInterval/clearInterval, and global (the global namespace object).
Q2. What is the difference between __dirname and __filename?
__dirname returns the directory path of the current module. For example, if file is at /home/user/app.js, __dirname is '/home/user'. __filename returns the full path including the filename: '/home/user/app.js'. Both are strings and are available in every module.
Q3. What is the process object in Node.js?
The process object is a global that provides information about and control over the current Node.js process. It includes properties like process.argv (command-line arguments), process.env (environment variables), process.pid (process ID), process.cwd() (current working directory), and methods like process.exit() and process.on() for event handling.
Q4. What is the global object in Node.js?
In Node.js, 'global' is the global namespace object, similar to 'window' in browsers. Variables declared without var/let/const become properties of global, but this is bad practice. Global properties are accessible everywhere. For example, global.myVar = 5 makes myVar available in all modules.
Q5. How do timer functions work in Node.js?
Node.js provides global timer functions: setTimeout(cb, delay) executes callback after delay, setInterval(cb, interval) repeats, and setImmediate(cb) executes at the end of current event loop cycle. Each returns an object that can be cleared with clearTimeout/clearInterval/clearImmediate. These are part of the global object.
