javascript / latest / errors / unexpected_type.html /

TypeError: "x" is (not) "y"

The JavaScript exception "x is (not) y" occurs when there was an unexpected type. Oftentimes, unexpected undefined or null values.

Message

TypeError: Unable to get property {x} of undefined or null reference (Edge)
TypeError: "x" is (not) "y" (Firefox)

Examples:
TypeError: "x" is undefined
TypeError: "x" is null
TypeError: "undefined" is not an object
TypeError: "x" is not an object or null
TypeError: "x" is not a symbol

Error type

What went wrong?

There was an unexpected type. This occurs oftentimes with undefined or null values.

Also, certain methods, such as Object.create() or Symbol.keyFor(), require a specific type, that must be provided.

Examples

Invalid cases

// undefined and null cases on which the substring method won't work
var foo = undefined;
foo.substring(1); // TypeError: foo is undefined

var foo = null;
foo.substring(1); // TypeError: foo is null

// Certain methods might require a specific type
var foo = {}
Symbol.keyFor(foo); // TypeError: foo is not a symbol

var foo = 'bar'
Object.create(foo); // TypeError: "foo" is not an object or null

Fixing the issue

To fix null pointer to undefined values, you can use the typeof operator, for example.

if (foo !== undefined) {
  // Now we know that foo is defined, we are good to go.
}

if (typeof foo !== 'undefined') {
  // The same good idea, but don't use this implementation - it can bring problems
  // because of the confusion between truly undefined and undeclared variables.
}

See also

© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Unexpected_type