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 1028720 of TypeError: "x" is read-only

  • Revision slug: Web/JavaScript/Reference/Errors/Read-only
  • Revision title: TypeError: "x" is read-only
  • Revision id: 1028720
  • Created:
  • Creator: jorendorff-moz
  • Is current revision? No
  • Comment mrrrgn made me do it

Revision Content

{{jsSidebar("Errors")}}

Message

TypeError: "x" is read-only (Firefox)
TypeError: 0 is read-only (Firefox)
TypeError: Cannot assign to read only property 'x' of #<object> (Chrome)
TypeError: Cannot assign to read only property '0' of [object Array] (Chrome)


Error type

{{jsxref("TypeError")}}

What went wrong?

The global variable or object property you're assigning to is a read-only property.

Read-only properties are not super common, but they can be created using {{jsxref("Object.defineProperty()")}} or {{jsxref("Object.freeze()")}}.

This error happens only in strict mode code. In non-strict code, the assignment is silently ignored.

Examples

Invalid cases

"use strict";
var obj = Object.freeze({name: "Elsa", score: 157});
obj.score = 0;  // TypeError

"use strict";
Object.defineProperty(this, "LUNG_COUNT", {value: 2, writable: false});
LUNG_COUNT = 3;  // TypeError

"use strict";
var frozenArray = Object.freeze([0, 1, 2]);
frozenArray[0]++;  // TypeError

The global variable undefined is also read-only, so you can't silence the infamous "undefined is not a function" error by doing this:

"use strict";
undefined = function () {};  // TypeError: "undefined" is read-only

Valid cases

"use strict";
var obj = Object.freeze({name: "Score", points: 157});
obj = {name: obj.name, points: 0};   // replacing it with a new object works

"use strict";
var LUNG_COUNT = 2;  // a `var` works, because it's not read-only
LUNG_COUNT = 3;  // ok (anatomically unlikely, though)

See also

  • {{jsxref("Object.defineProperty()")}}
  • {{jsxref("Object.freeze()")}}
</object>

Revision Source

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

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

<pre class="syntaxbox">
TypeError: "x" is read-only (Firefox)
TypeError: 0 is read-only (Firefox)
TypeError: Cannot assign to read only property 'x' of #<Object> (Chrome)
TypeError: Cannot assign to read only property '0' of [object Array] (Chrome)
</pre>

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

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

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

<p>The global variable or object property you're assigning to is a read-only property.</p>

<p>Read-only properties are not super common, but they can be created using {{jsxref("Object.defineProperty()")}} or {{jsxref("Object.freeze()")}}.</p>

<p>This error happens only in strict mode code. In non-strict code, the assignment is silently ignored.</p>

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

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

<pre class="brush: js example-bad">
"use strict";
var obj = Object.freeze({name: "Elsa", score: 157});
obj.score = 0;  // TypeError

"use strict";
Object.defineProperty(this, "LUNG_COUNT", {value: 2, writable: false});
LUNG_COUNT = 3;  // TypeError

"use strict";
var frozenArray = Object.freeze([0, 1, 2]);
frozenArray[0]++;  // TypeError
</pre>

<p>The global variable <code>undefined</code> is also read-only, so you can't silence the infamous "undefined is not a function" error by doing this:</p>

<pre class="brush: js example-bad">
"use strict";
undefined = function () {};  // TypeError: "undefined" is read-only
</pre>

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

<pre class="brush: js example-good">
"use strict";
var obj = Object.freeze({name: "Score", points: 157});
obj = {name: obj.name, points: 0};   // replacing it with a new object works

"use strict";
var LUNG_COUNT = 2;  // a `var` works, because it's not read-only
LUNG_COUNT = 3;  // ok (anatomically unlikely, though)
</pre>

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

<ul>
 <li>{{jsxref("Object.defineProperty()")}}</li>
 <li>{{jsxref("Object.freeze()")}}</li>
</ul>
Revert to this revision