Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 960221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:08:55+00:00 2026-05-16T01:08:55+00:00

What is the most appropriate way to test if a variable is undefined in

  • 0

What is the most appropriate way to test if a variable is undefined in JavaScript?

I’ve seen several possible ways:

if (window.myVariable)

Or

if (typeof(myVariable) != "undefined")

Or

if (myVariable) // This throws an error if undefined. Should this be in Try/Catch?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:08:55+00:00Added an answer on May 16, 2026 at 1:08 am

    If you are interested in finding out whether a variable has been declared regardless of its value, then using the in operator is the safest way to go. Consider this example:

    // global scope
    var theFu; // theFu has been declared, but its value is undefined
    typeof theFu; // "undefined"
    

    But this may not be the intended result for some cases, since the variable or property was declared but just not initialized. Use the in operator for a more robust check.

    "theFu" in window; // true
    "theFoo" in window; // false
    

    If you are interested in knowing whether the variable hasn’t been declared or has the value undefined, then use the typeof operator, which is guaranteed to return a string:

    if (typeof myVar !== 'undefined')
    

    Direct comparisons against undefined are troublesome as undefined can be overwritten.

    window.undefined = "foo";
    "foo" == undefined // true
    

    As @CMS pointed out, this has been patched in ECMAScript 5th ed., and undefined is non-writable.

    if (window.myVar) will also include these falsy values, so it’s not very robust:

    false
    0
    ""
    NaN
    null
    undefined
    

    Thanks to @CMS for pointing out that your third case – if (myVariable) can also throw an error in two cases. The first is when the variable hasn’t been defined which throws a ReferenceError.

    // abc was never declared.
    if (abc) {
        // ReferenceError: abc is not defined
    } 
    

    The other case is when the variable has been defined, but has a getter function which throws an error when invoked. For example,

    // or it's a property that can throw an error
    Object.defineProperty(window, "myVariable", { 
        get: function() { throw new Error("W00t?"); }, 
        set: undefined 
    });
    if (myVariable) {
        // Error: W00t?
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the most appropriate way to serialize Java Classes to XML? I tried
I'm looking for the simplest or most appropriate way on Mac OS X to
What is the most appropriate way of getting the mouse cursor position or button
what would be the most appropriate way to archieve something like the template variables
I am unclear what's the most appropriate way to represent simple data objects with
I'm looking for the best practise or the most appropriate way to achieve auto-suggestion
What's the most appropriate way to detect if a socket has been dropped or
I am looking for the most appropriate way of dealing with a user activity
In a controller, what is the most appropriate way to call the action of
I am not sure what the most appropriate way to handle JPA objects that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.