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 8585121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:58:56+00:00 2026-06-11T21:58:56+00:00

Is it unsafe to compare a variable against undefined? if(foo == undefined) vs if(foo

  • 0

Is it unsafe to compare a variable against undefined?

if(foo == undefined)

vs

if(foo == 'undefined')

Is the first example sufficient? Or should it be something like

if('undefined' in window){
     //compare against undefined
} else {
    //compare against 'undefined'
}

since undefined exists on the window object? Will it exist in all browsers? Or should I simply compare to == 'undefined'? I’ve found some similar questions on SO, but no answers regarding the existance of the undefined property on the window object.

  • 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-06-11T21:58:57+00:00Added an answer on June 11, 2026 at 9:58 pm

    I think you’re getting mixed up between foo == undefined and typeof foo == "undefined".

    Both will yield the same result unless the variable undefined has been set to something else in the current scope. In this case, foo == undefined will compare against that, where-as typeof foo == "undefined" will still resolve correctly.

    var undefined = 4;
    var reallyUndefined;
    
    reallyUndefined == undefined; // false
    typeof reallyUndefined == undefined; // true
    

    Whether it’s a real world scenario that undefined will ever be set to something else is debatable, and I’d question the validity of the library/ code that does that… Because of this however, it’s deemed good practise to always use typeof foo === "undefined".

    I’d also be wary about using foo == undefined against foo === undefined (note the triple equals, which does not use type-coercian, compared to == which does).

    Using ==, you run the risk of things like null == undefined; // true, where-as null === undefined; // false. This is a good example of why you should always use ===.


    tl;dr: typeof foo === "undefined";

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example: javac Foo.java Note: Foo.java uses unchecked or unsafe operations. Note: Recompile with
What are the limitations of unsafe code, in C#? For example, can I do
Read that the following code is an example of unsafe construction as it allows
Suppose I have: unsafe { byte* start = GetStartLocation(); int something = start[4]; }
Is this approach unsafe? #include <tr1/memory> Foo * createFoo() { return new Foo(5); }
Can someone give an example of a good time to actually use unsafe and
I've imported an API function like [DllImport(gdi32.dll)] private unsafe static extern bool SetDeviceGammaRamp(Int32 hdc,
Supposedly weak is like assign or unsafe_unretained , except that a weak variable is
Ignoring unsafe code, .NET cannot have memory leaks. I've read this endlessly from many
I have this unsafe code that I need to make safe so it can

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.