Thursday, September 4, 2025
HomeLanguagesJavascriptJavaScript TypeError – Can’t assign to property “X” on “Y”: not an...

JavaScript TypeError – Can’t assign to property “X” on “Y”: not an object

This JavaScript exception can’t assign to property occurs in strict-mode only and this error occurs If the user tries to create a property on any of the primitive values like a symbol, a string, a number, or a boolean. Primitive values cannot be used to hold any property.

Message:

TypeError: can't assign to property "x" on {y}: not an 
           object (Firefox)
TypeError: Cannot create property 'x' on {y} (Chrome)

Error Type:

TypeError

Cause of Error: In strict-mode, a primitive value in the code is used to create a property on it. Primitive values can not hold properties.

Example 1: In this example, the string is used to create properties on it, So the error has occurred.

Javascript




'use strict';
let GFG = "This is neveropen";
GFG.prop = {}; // error here


Output(in console):

TypeError: Cannot create property 'prop' on string 
'This is neveropen'

Example 2: In this example, the boolean ‘true’ is used to create properties on it, So the error has occurred.

Javascript




'use strict';
let variableName = true;
variableName.prop = {}; // error here


Output(in console):

TypeError: Cannot create property 'prop' on boolean 'true'
RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS