How do you check whether a variable is defined and initialized in JavaScript? | A1Lab@FahadM to JavaScriptEnglish • 6 days ago

How do you check whether a variable is defined and initialized in JavaScript?

message-square
0

Checking whether a variable is “defined and initialized” in JavaScript depends on whether you mean an in-scope variable whose value is not undefined (and optionally not null), or a global/undeclared identifier that might throw a ReferenceError. For declared variables or properties in scope, value !== undefined (or value != null when you also reject null) is the clearest check.


1. Checking Declared Variables and Values (!== undefined vs. != null)

Once a variable or parameter is in scope, check whether it holds an initialized value rather than undefined:

function greet(userName) {
  // Initialized to a non-undefined value (note: "" or 0 is still initialized)
  if (userName !== undefined) {
    console.log(`Hello, ${userName}`);
  }
}

let configValue; // Declared, not initialized -> undefined
console.log(configValue !== undefined); // false

configValue = "production"; // Initialized
console.log(configValue !== undefined); // true

If you want to treat both undefined and null as uninitialized/missing, use loose inequality against null (!= null), which catches both null and undefined in one check:

function hasValue(value) {
  return value != null;
}

console.log(hasValue(undefined)); // false
console.log(hasValue(null));      // false
console.log(hasValue(0));         // true
console.log(hasValue(""));        // true
console.log(hasValue(false));     // true

2. Checking Undeclared Identifiers or Global Properties Safely

Referencing an undeclared identifier directly (typeof undeclaredVar) throws a ReferenceError for let/const in the Temporal Dead Zone, while typeof on an undeclared identifier returns "undefined" without throwing:

// Safe check for an undeclared identifier without throwing ReferenceError
if (typeof maybeGlobal !== "undefined") {
  console.log("Declared and not undefined:", maybeGlobal);
}

// Checking a property on an object or globalThis
const hasDebugFlag = typeof globalThis.DEBUG !== "undefined";
console.log(hasDebugFlag);

3. Common Traps to Avoid

  • Using truthiness (if (x)) to test initialization: 0, "", false, and NaN are initialized values that evaluate as falsy—if (x) falsely treats them as missing.
  • Assuming typeof prevents ReferenceError on let or const before initialization: Accessing a let or const variable before its declaration line in the same block still throws a ReferenceError (Temporal Dead Zone).
  • Confusing null and undefined: undefined usually means uninitialized, omitted, or missing, whereas null is typically an explicit “intentionally empty” value—pick !== undefined or != null based on what your code means by uninitialized.

TL;DR: Use value !== undefined for declared variables/parameters (or value != null to exclude both undefined and null), use typeof name !== "undefined" only when guarding against undeclared identifiers, and avoid truthiness checks (if (value)) which reject valid falsy values like 0 or "".

Are you checking function parameters/local variables, guarding optional object/global properties, or deciding how to treat null vs. falsy values like 0 and ""?