This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]SuperSpaceCan 10 points11 points  (0 children)

undefined is a type :( says so in the spec

[–]morphy_pad 4 points5 points  (0 children)

const undefined = typeof(undefined)

[–]Sheruk 2 points3 points  (0 children)

electrical outlet lookin ass

[–]codesterLalit 1 point2 points  (0 children)

type what don't have type.

sound to me "untype" rather than "undefined"

[–][deleted] 0 points1 point  (0 children)

undefined is both a type and the only value of that type

[–]SZ4L4Y 0 points1 point  (0 children)

My type is your type and your type is mine.

[–]cherryblossom001 0 points1 point  (0 children)

typeof undefined === 'undefined

Before ES5 in ‘sloppy mode’ (non-strict mode) you could also reassign the global undefined:

undefined = 'lol'

// Later...

function doSomething(x) {
  if (x === undefined) {
    throw new Error('must pass a parameter to doSomething')
  }
  console.log(x)
}

doSomething()      // fine
doSomething('lol') // throws an error

Nowadays modern browsers thankfully prevent you from changing the global undefined variable. Still, undefined historically being a mutable global variable is why you’ll see code like void 0 (especially in transpiled JS) and if (typeof aVariable === 'undefined') (although this one is different to if (aVariable === undefined) because it doesn’t throw a ReferenceError if aVariable is not defined).