Skip to main content
Stack Overflow
  1. About
  2. For Teams

Timeline for Detecting an undefined object property

Current License: CC BY-SA 3.0

25 events
when toggle format what by license comment
Sep 1, 2022 at 8:51 comment added strix25 do you need to check on window object if typeof myVar === "undefined" or is it obsolete if you use if(myvar) {}
Jul 24, 2020 at 23:38 comment added Peter Mortensen But it is no longer relevant with ECMAScript 5 (and later)?
Mar 17, 2020 at 21:13 comment added CherryDT ^ totally agree. someone who might shadow undefined might also shadow Math or assign Function.prototype.apply or anything else that you take for granted! Please. ^ totally agree. someone who might shadow undefined might also shadow Math or assign Function.prototype.apply or anything else that you take for granted! Please. How about Object.prototype.toJSON=()=>({rick:'rolled'})? JSON.stringify({hello:'world'}) === '{"rick":"rolled"}' then. There are so many ways to break the expected JavaScript environment, you can't write code that works around all of those things.
Dec 18, 2017 at 13:39 comment added user8897421 Funny that people suggest these silly and error-prone hacks to avoid a shadowed undefined (which could only be done by a terrible developer), yet they blithely use other global identifiers that could also have been shadowed. Bizarre. Just bizarre.
Aug 16, 2017 at 0:17 comment added Ry- @MarkPflug: It didn’t make sense in 2010 either, sorry. undefined is not a keyword in 2017. You can still use var undefined = false;. The answer to this has always been "don’t do that" rather than "use convoluted workarounds for something that is never the case".
Aug 15, 2017 at 20:52 history edited Dave Jarvis CC BY-SA 3.0
Emphasis. Moved correct answer to the top.
Jul 31, 2017 at 13:02 comment added Atav32 Just from experience, I think you really want your code to throw that ReferenceError if a variable is not declared. It's so frustrating when things silently fail.
Jul 25, 2017 at 12:36 comment added Velu S Gautam since writable property of undefined is set to false. even if you assign false to undefined it will not get set. so undefined will be "undefined" always not false so your answer makes no sense. You can check that in ECMA script specification for JavaScript. ecma-international.org/ecma-262/5.1/#sec-15.1.1.3
Nov 23, 2016 at 5:16 comment added Codebeat Rule X: NEVER use reserved words as variablename, functionname or whatever. Don't understand you get so many positive votes. Because it is possible it doesn't mean it's legal, besides, it can change in future and your code (and everything rely on it) doesn't work any longer. This is a bad/unreliable example and shows you how not do it.
Dec 15, 2015 at 15:38 comment added Zero3 This answer is incorrect as well. The question was about undefined object properties, not undefined variables. There is a significant difference. It is, for example, perfectly reasonable to do if (obj.field === undefined). I think the risk of someone doing var undefined = false; is overrated. You will have to program unreasonably defensive if you want to protect against all such kinds of side effects caused by poor programming.
Nov 1, 2015 at 12:53 comment added Stijn de Witt @KevinMeredith "Is the "!=" acceptable?" It certainly is. In my book it is better than !== as it's shorter and the longer forms offers no benefits i.c.w. typeof.
Oct 2, 2015 at 19:12 comment added dharcourt @Stijn: While it's not possible to modify the global undefined, it's still possible to redefine it locally as is shown in this answer. Copy and paste the answer's sample code into a modern browser and you'll see that myVar === undefined can still cause problems.
Jan 16, 2015 at 19:18 comment added rojobuffalo fiddle for demonstration of this point
Nov 9, 2014 at 12:08 history edited Peter Mortensen CC BY-SA 3.0
Copy edited (e.g. ref. <http://en.wikipedia.org/wiki/JavaScript>).
Feb 7, 2014 at 14:09 comment added user247702 In modern browsers (FF4+, IE9+, Chrome unknown), it's no longer possible to modify undefined. MDN: undefined
Oct 3, 2013 at 17:45 comment added Claudiu Also keep in mind you can always do void 0 to get the value that undefined points to. So you can do if (myVar === void 0). the 0 isn't special, you can literally put any expression there.
Aug 23, 2013 at 22:00 comment added Ingo Bürk @Kevin Yes, it would be, because typeof will always return a string, making the type-safe comparison unnecessary. It's just considered a good practice to use === resp. !== exclusively. Actually I think that jQuery uses only !=.
Aug 20, 2013 at 15:12 comment added eis in addition to Marks comments, I don't get this: "myVar === undefined will raise an error in the situation where myVar is undeclared." - why is this bad? Why would I not want to have an error if I'm referencing undeclared variables?
Jul 29, 2013 at 4:35 history edited Kevin Burke CC BY-SA 3.0
deleted 1 characters in body
Jul 5, 2013 at 16:17 comment added Kevin Meredith What about if(typeof myVar != "undefined"). Is the "!=" acceptable?
Nov 8, 2012 at 6:49 comment added Codebeat I agree that undefined is not a keyword, undefined is a predefined variable with an undefined value. Something like alert, this is a function that you can override with something else. Alert is also not a keyword but a part of the standard function set, ap part of the window object that has a global scope. Like undefined you can also override the alert function by re-assigning it. For example: window.alert = function(s) { console.log(s); } is also a legal construction.
Nov 8, 2011 at 18:26 history edited allyourcode CC BY-SA 3.0
Changed comments and string in code.
S Jun 15, 2011 at 0:34 history suggested Simon E. CC BY-SA 3.0
Clarified that posted code is NOT a good example
Jun 15, 2011 at 0:20 review Suggested edits
S Jun 15, 2011 at 0:34
Aug 23, 2010 at 18:03 history answered MarkPflug CC BY-SA 2.5
toggle format

AltStyle によって変換されたページ (->オリジナル) /