Globals — Node.js Technical Interview Questions & Answers

Globals

Globals

  • Globals you actually use: console, process, Buffer, setTimeout, __dirname / __filename in CommonJS. In ESM, __dirname is not free — use import.meta.url.
  • process.env.PORT for config. Don’t dump secrets in source.
  • Trap: assuming window exists in Node. That’s the browser.

Technical Interview Tip: process.env.PORT. No window. __dirname in CommonJS.

Code Examples for Globals

Globals — working example

// Globals
const users = [
  { id: 1, name: "Asha" },
  { id: 2, name: "Ravi" },
];

function findUser(id) {
  return users.find((u) => u.id === id) ?? null;
}

console.log(findUser(2));
console.log(users.map((u) => u.name));

Globals: find user 2, then list names. Say both prints.

How to Explain Globals to an Interviewer

Is window in Node?

No That’s the browser