Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. JavaScript error reference
  5. TypeError: can't assign to property "x" on "y": not an object

TypeError: can't assign to property "x" on "y": not an object

The JavaScript strict mode exception "can't assign to property" occurs when attemptingto create a property onprimitive valuesuch as asymbol, astring, anumber or aboolean.Primitive values cannot hold anyproperty.

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

TypeError.

What went wrong?

Instrict mode, aTypeError is raised when attempting tocreate a property onprimitive value suchas asymbol, astring, anumber or aboolean.Primitive values cannot hold anyproperty.

The problem might be that an unexpected value is flowing at an unexpected place, orthat an object variant of aString or aNumber is expected.

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 theprimitive from being used in such places, or fix the issue by creating the object equivalentObject.

js
"use strict";const foo = new String("my string");foo.bar = {};

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp