--- title: 'TypeError: can''t assign to property "x" on "y": not an object' slug: Web/JavaScript/Reference/Errors/Cant_assign_to_property translation_of: Web/JavaScript/Reference/Errors/Cant_assign_to_property ---
{{jsSidebar("Errors")}}
Ошибка строгого режима JavaScript "can't assign to 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

{{jsxref("TypeError")}}.

What went wrong?

In {{jsxref("Strict_mode")}}, a {{jsxref("TypeError")}} is raised when attempting to create a property on primitive value such as a symbol, a string, a number or a boolean. Primitive values cannot hold any property.

The problem might be that an unexpected value is flowing at an unexpected place, or that an object variant of a {{jsxref("String")}} or a {{jsxref("Number")}} is expected.

Examples

Invalid cases

'use strict';

var 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 primitive from being used in such places, or fix the issue is to create the object equivalent {{jsxref("Object")}}.

'use strict';

var foo = new String("my string");
foo.bar = {};

See also