Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1062302 of TypeError: "x" is (not) "y"

  • Revision slug: Web/JavaScript/Reference/Errors/Unexpected_type
  • Revision title: TypeError: "x" is (not) "y"
  • Revision id: 1062302
  • Created:
  • Creator: fscholz
  • Is current revision? Yes
  • Comment

Revision Content

{{jsSidebar("Errors")}}

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

{{jsxref("TypeError")}}.

What went wrong?

There was an unexpected type. This occurs oftentimes with {{jsxref("undefined")}} or {{jsxref("null")}} values.

Also, certain methods, such as {{jsxref("Object.create()")}} or {{jsxref("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 or null values, you can use the typeof operator, for example.

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

See also

  • {{jsxref("undefined")}}
  • {{jsxref("null")}}

Revision Source

<div>{{jsSidebar("Errors")}}</div>

<h2 id="Message">Message</h2>

<pre class="syntaxbox">
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
</pre>

<h2 id="Error_type">Error type</h2>

<p>{{jsxref("TypeError")}}.</p>

<h2 id="What_went_wrong">What went wrong?</h2>

<p>There was an unexpected type. This occurs oftentimes with {{jsxref("undefined")}} or {{jsxref("null")}} values.</p>

<p>Also, certain methods, such as {{jsxref("Object.create()")}} or {{jsxref("Symbol.keyFor()")}}, require a specific type, that must be provided.</p>

<h2 id="Examples">Examples</h2>

<h3 id="Invalid_cases">Invalid cases</h3>

<pre class="brush: js example-bad">
// 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
</pre>

<h3 id="Fixing_the_issue">Fixing the issue</h3>

<p>To fix null pointer to <code>undefined</code> or <code>null</code> values, you can use the <a href="/en-US/docs/Web/JavaScript/Reference/Operators/typeof">typeof</a> operator, for example.</p>

<pre class="brush: js">
if (typeof foo !== 'undefined') {
  // Now we know that foo is defined, we are good to go.
}</pre>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("undefined")}}</li>
 <li>{{jsxref("null")}}</li>
</ul>
Revert to this revision