How to check if a variable is undefined in JavaScript

Checking for undefined variables is crucial for preventing runtime errors, validating function parameters, and handling optional values safely. As the creator of CoreUI with over 25 years of JavaScript development experience, I’ve implemented undefined checks extensively in component prop validation, API response handling, and defensive programming patterns. From my expertise, the most reliable approach is using strict equality comparison with undefined or the typeof operator for comprehensive undefined detection. This technique ensures robust code that handles missing or uninitialized values gracefully.

Use strict equality === undefined or typeof operator to check specifically for undefined values.

function isUndefined(value) {
  return value === undefined
}

console.log(isUndefined(undefined)) // true
console.log(isUndefined(null))      // false
console.log(isUndefined(0))         // false

Here value === undefined uses strict equality to check if the variable is exactly undefined. This approach distinguishes undefined from other falsy values like null, 0, false, or empty strings. Alternatively, typeof value === 'undefined' works even for undeclared variables without throwing ReferenceError. The strict comparison ensures precise undefined detection for proper conditional logic and error prevention.

Best Practice Note:

This is the same approach we use in CoreUI components for prop validation and optional parameter handling. Use strict equality for defined variables, or typeof for potentially undeclared variables to avoid ReferenceError exceptions in your undefined checks.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author