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 1082480 of ReferenceError: reference to undefined property "x"

  • Revision slug: Web/JavaScript/Reference/Errors/Undefined_prop
  • Revision title: ReferenceError: reference to undefined property "x"
  • Revision id: 1082480
  • Created:
  • Creator: Sheppy
  • Is current revision? Yes
  • Comment Minor text cleanup; added a little more explanatory text

Revision Content

{{jsSidebar("Errors")}}

Message

ReferenceError: reference to undefined property "x" (Firefox)

Error type

{{jsxref("ReferenceError")}} warning in strict mode only.

What went wrong?

The script attempted to access an object property which doesn't exist. There are two ways to access properties; see the property accessors reference page to learn more about them.

Errors about references to undefined properties occur in strict mode code only. In non-strict code, accesses to properties that don't exist are silently ignored.

Examples

Invalid cases

In this case, the property bar is an undefined property, so a ReferenceError will occur.

"use strict"; 

var foo = {};
foo.bar; // ReferenceError: reference to undefined property "bar"

Valid cases

To avoid the error, you need to either add a definition for bar to the object or check for the existence of the bar property before trying to access it; one way to do that is to use the {{jsxref("Object.prototype.hasOwnProperty()")}} method), like this:

"use strict";

var foo = {};

// Define the bar property

foo.bar = "moon";
console.log(foo.bar); // "moon"

// Test to be sure bar exists before accessing it

if (foo.hasOwnProperty("bar") {
  console.log(foo.bar);
}

See also

Revision Source

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

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

<pre class="syntaxbox">
ReferenceError: reference to undefined property "x" (Firefox)
</pre>

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

<p>{{jsxref("ReferenceError")}} warning in <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict mode</a> only.</p>

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

<p>The script attempted to access an object property which doesn't exist. There are two ways to access properties; see the <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors">property accessors</a> reference page to learn more about them.</p>

<p>Errors about references to undefined properties occur in <a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">strict mode code</a> only. In non-strict code, accesses to properties that don't exist are silently ignored.</p>

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

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

<p>In this case, the property <code>bar</code> is an undefined property, so a <code>ReferenceError</code> will occur.</p>

<pre class="brush: js example-bad">
"use strict"; 

var foo = {};
foo.bar; // ReferenceError: reference to undefined property "bar"
</pre>

<h3 id="Valid_cases">Valid cases</h3>

<p>To avoid the error, you need to either add a definition for <code>bar</code> to the object or check for the existence of the <code>bar</code> property before trying to access it; one way to do that is to use the {{jsxref("Object.prototype.hasOwnProperty()")}} method), like this:</p>

<pre class="brush: js example-good">
"use strict";

var foo = {};

// Define the bar property

foo.bar = "moon";
console.log(foo.bar); // "moon"

// Test to be sure bar exists before accessing it

if (foo.hasOwnProperty("bar") {
  console.log(foo.bar);
}</pre>

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

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Strict_mode">Strict mode</a></li>
</ul>
Revert to this revision