TypeError: can't assign to property "x" on "y": not an object
The JavaScript strict mode exception "can't assign to property" occurs when attempting to create a property on primitive value such as a boolean. property.
Message
TypeError: Cannot create property 'x' on number '1' (V8-based) TypeError: can't assign to property "x" on 1: not an object (Firefox) TypeError: Attempted to assign to readonly property. (Safari)
Error type
Examples
Invalid cases
js
"use strict";
const foo = "my string";
/ The following line does nothing if not in strict mode.
foo.bar = {}; / TypeError: can't assign to property "bar" on "my string": not an object
Fixing the issue
Either fix the code to prevent the Object
.
js
"use strict";
const foo = new String("my string");
foo.bar = {};