-3

I'm wondering why is JavaScript's undefined created as object (W3Schools) and not defined as a keyword? And, are there any advantages to create custom undefined object (overwriting default undefined)?

For me, it's little bit confusing - as I've read, I should use

if(myObject === 'undefined')
{ /* ... */ }

although I often use incorrect version with === undefined). But this approach seems to me like I try to compare myObject with String though I'm trying to determine myObject state.

So, is it conceptual mistake or there was some valid reason to create it this way?

And last question: is using incorrect version potentially vulnerable? (by JS modification from browser or browser plugin)

Thanks for all responses.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
Pavel Pája Halbich
  • 1,529
  • 2
  • 17
  • 22
  • Are you referring to typeof(myObject)? undefined is a valid type, try: a = undefined; typeof(a); a will be undefined as a type, its type will be "undefined" – Digits Apr 17 '15 at 20:41
  • You should use: `if(typeof myObject == 'undefined')` – hindmost Apr 17 '15 at 20:41
  • http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property – Code Whisperer Apr 17 '15 at 20:41
  • what is wrong with if(myObject == undefined) – bhspencer Apr 17 '15 at 20:42
  • w3schools is better than it used to be, but it's still not preferred over various other sources like [MDN](https://developer.mozilla.org/en-US/) – Pointy Apr 17 '15 at 20:44
  • To be fair w3schools never claims it's an object. It's listed under "JavaScript Objects, Properties, and Methods," not sure why the OP picked "object" out of that list and not "property". – JJJ Apr 17 '15 at 20:47
  • 2
    How stupid to downvote this question. This kind of knowledge seeking is exactly what Stack Overflow is for. – A. Duff Apr 17 '15 at 21:04
  • I think you are using the term "object" incorrectly here. You seem to be asking why `undefined` is a **variable** instead of a keyword. – Felix Kling Apr 17 '15 at 21:20

2 Answers2

3

It's not an object; that's just an example of why even today w3schools is a poor resource.

The undefined "value" (it's problematic to call it a value as it's more like the lack of a value) is a marker indicating that something just isn't present. It's somewhat odd that it's distinct from null, but it is.

The way you're testing for undefined is incorrect. You should either compare directly to undefined:

if (something === undefined) 

or compare by type:

if (typeof something === "undefined")

It's very often the case that you don't really care whether something is undefined or null, in which case you can safely do this:

if (something == null)

because undefined is treated as being the same as null in an == comparison. (If you're a believer in the "never use ==" religion, then obviously you wouldn't do that.)

Pointy
  • 405,095
  • 59
  • 585
  • 614
0

In most recent JS implementations, undefined cannot be redefined (you could before). undefined = object will still return undefined. Previously you could redefine undefined however that could cause compatibility issues if another library/script ALSO redefined undefined OR if it expected undefined to really be undefined. It is(was) thus a "really bad idea" to redefine undefined.

To compare against undefined correctly you have to do object === undefined not object == "undefined". "undefined" between the quotes is a String.

There are other ways (typeof object == "undefined") since typeof returns a String; that solution is a bit more obtuse to read and easier to make a mistake in my personal opinion but compatible with older JS implementations.

You can do object = undefined to empty the variable, that's why it's there.

Guru Evi
  • 184
  • 5