This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

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

Message

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

    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

TypeError.

What went wrong?

๊ทธ๊ฒƒ์€ ์ •ํ™•ํ•˜์ง€ ์•Š์€ ํ˜•ํƒœ์ด๋‹ค. ๊ทธ๊ฒƒ์€ ๊ฐ€๋”undefined ๋‚˜ null ๊ฐ’์„ ๋ฐœ์ƒํ•œ๋‹ค.

๋˜ํ•œ, Object.create() ๋˜๋Š” Symbol.keyFor()์™€ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋Š” ๋ฐ˜๋“œ์‹œ ์ œ๊ณต๋˜์–ด์•ผํ•˜๋Š” ํŠน๋ณ„ํ•œ ํ˜•ํƒœ๋ฅผ ์š”๊ตฌํ•œ๋‹ค.

Examples

Invalid cases

js
// 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

undefined ๋‚˜ null ๊ฐ’์„ ๊ฐ€์ง„ null ํฌ์ธํ„ฐ๋ฅผ ๊ณ ์น˜๊ธฐ ์œ„ํ•ด์„œ ์•„๋ž˜ ์˜ˆ์ œ์™€ ๊ฐ™์ด typeof ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

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

๊ฐ™์ด ๋ณด๊ธฐ