One data grid for JavaScript, React, Vue & Angular. Sorting, filtering, virtualization, pinning, and CSV export included. Early access $199 $349 → [Get it now]

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, 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

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to Detect a Click Outside of a React Component
How to Detect a Click Outside of a React Component

CSS Selector for Parent Element
CSS Selector for Parent Element

How to Convert a Map to an Array in JavaScript
How to Convert a Map to an Array in JavaScript

How to Center a Button in CSS
How to Center a Button in CSS

Answers by CoreUI Core Team