TypeError: «x» is read-only
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.
Message
TypeError: Cannot assign to read only property 'x' of # (V8-based) TypeError: "x" is read-only (Firefox) TypeError: Attempted to assign to readonly property. (Safari)
Error type
What went wrong?
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.
Examples
Invalid cases
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
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'; let obj = Object.freeze(< name: 'Score', points: 157 >); obj = < name: obj.name, points: 0 >; // replacing it with a new object works
See also
Found a problem with this page?
Last modified: Jul 16, 2022 , by MDN contributors
JavaScript
The JavaScript exception «precision out of range» occurs when number that’s outside 0 and 20 (or 21) was passed into toFixed toPrecision.
The JavaScript exception «Permission denied to access property» occurs when there was an attempt object for which you have no DOMException.
The JavaScript exception «redeclaration of formal parameter» occurs when same variable name function and is then redeclared using let assignment body again.
TypeError: «x» is read-only
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.
Message
TypeError: Cannot assign to read only property 'x' of # (V8-based) TypeError: "x" is read-only (Firefox) TypeError: Attempted to assign to readonly property. (Safari)
Error type
What went wrong?
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.
Examples
Invalid cases
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
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'; let obj = Object.freeze( name: 'Score', points: 157 >); obj = name: obj.name, points: 0 >; // replacing it with a new object works
See also
Found a problem with this page?
Last modified: Jul 16, 2022 , by MDN contributors
TypeError: «x» is read-only
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.
Message
TypeError: Cannot assign to read only property 'x' of # (V8-based) TypeError: "x" is read-only (Firefox) TypeError: Attempted to assign to readonly property. (Safari)
Error type
What went wrong?
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.
Examples
Invalid cases
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
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"; let obj = Object.freeze( name: "Score", points: 157 >); obj = name: obj.name, points: 0 >; // replacing it with a new object works
See also
Found a content problem with this page?
This page was last modified on Jul 7, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
TypeError: «x» is read-only
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.
Message
TypeError: Assignment to read-only properties is not allowed in strict mode (Edge) 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
What went wrong?
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.
Examples
Invalid cases
Read-only properties are not super common, but they can be created using Object.defineProperty() or Object.freeze() .
'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
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
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)