How can I check for undefined in JavaScript

0 votes

What is the most suitable and accurate way to test if a variable is undefined while using JavaScript? Have shared three possible ways below:

if (window.myVariable)

Or

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

Or

if (myVariable) // This throws an error if undefined. Should this be in Try/Catch?

Feb 22, 2022 in Java by Rahul
• 9,680 points
1,189 views

1 answer to this question.

0 votes

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. Shown in the example below:

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

However, this could not be the expected result for some cases, since the variable or property was declared but still, was not initialized. Use the in operator for a more robust check as mentioned below:-

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

If you are keen in finding out whether the variable hasn't been declared or has the value undefined, then use the typeof operator, which is guaranteed to return a string as mentioned below:-


if (typeof myVar !== 'undefined')

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

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

This has been patched in ECMAScript 5th ed., and undefined is not-writable:-

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

false

0

""

NaN

null
undefined

However, 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? 
}

answered Feb 22, 2022 by Aditya
• 7,680 points

Related Questions In Java

0 votes
1 answer

I am learning looping statements. Can you tell me how 'for-each' works in Java?

While programming we often write code that ...READ MORE

answered Apr 17, 2018 in Java by Rishabh
• 3,620 points
949 views
0 votes
0 answers

How can I validate an email address in JavaScript?

To avoid the most basic mistyping, I'd ...READ MORE

Sep 20, 2022 in Java by Nicholas
• 7,760 points
454 views
0 votes
0 answers

How do I check if an array includes a value in JavaScript?

What is the shortest and most efficient ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
611 views
0 votes
0 answers

How to check 'undefined' value in jQuery

How we can add a check for ...READ MORE

Jul 28, 2022 in Web Development by gaurav
• 23,260 points
1,851 views
0 votes
1 answer

Presenting docket dtates inside html page by javascript

Use the Docker Engine Api:Docker Engine API ...READ MORE

answered Jun 20, 2018 in Docker by DareDev
• 6,890 points
745 views
0 votes
1 answer

Migrating proxy npm repo in nexus 3

I don't think you can achieve this ...READ MORE

answered Jun 22, 2018 in DevOps Tools by DareDev
• 6,890 points
1,527 views
+1 vote
1 answer

What is the difference between JavaScript and Java

This quote rightly explains that 2 totally ...READ MORE

answered Jun 29, 2018 in Java by Daisy
• 8,140 points
793 views
0 votes
1 answer

How do I get the current date in JavaScript?

To ensure that you get the current ...READ MORE

answered Feb 18, 2022 in Java by Aditya
• 7,680 points
592 views
0 votes
1 answer

How do I redirect with JavaScript?

To redirect to another page by Using ...READ MORE

answered Feb 16, 2022 in Java by Aditya
• 7,680 points
547 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP