| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Read-only | [Back] [Original] |
Get to know MDN better
The JavaScript strict mode-only exception "is read-only" occurs when a global variable or object property that was assigned to is a read-only property.
TypeError: Cannot assign to read only property 'x' of #<Object> (V8-based) TypeError: "x" is read-only (Firefox) TypeError: Attempted to assign to readonly property. (Safari)
TypeError in strict mode only.
The global variable or object property that was assigned to is a read-only property. (Technically, it is a non-writable data property.)
This error happens only in strict mode code. In non-strict code, the assignment is silently ignored.
Read-only properties are not super common, but they can be created using
Object.defineProperty() or Object.freeze().
"use strict";
const 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");
const frozenArray = Object.freeze([0, 1, 2]);
frozenArray[0]++; // TypeError
There are also a few read-only properties built into JavaScript. Maybe you tried to redefine a mathematical constant.
"use strict";
Math.PI = 4; // TypeError
Sorry, you can't do that.
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
"use strict";
let obj = Object.freeze({ name: "Score", points: 157 });
obj = { name: obj.name, points: 0 }; // replacing it with a new object works
This page was last modified on Jul 8, 2025 by MDN contributors.
Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |