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 GeeksforGeeks" ;
GFG.prop = {};
|
Output(in console):
TypeError: Cannot create property 'prop' on string
'This is GeeksforGeeks'
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 = {};
|
Output(in console):
TypeError: Cannot create property 'prop' on boolean 'true'
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
02 Jun, 2023
Like Article
Save Article