-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
60 lines (41 loc) · 2.27 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
---
title: 'TypeError: can''t assign to property "x" on "y": not an object'
slug: Web/JavaScript/Reference/Errors/Cant_assign_to_property
page-type: javascript-error
---
{{jsSidebar("Errors")}}
The JavaScript strict mode exception "can't assign to property" occurs when attempting
to create a property on [primitive](/en-US/docs/Glossary/Primitive) value
such as a [symbol](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol), a [string](/en-US/docs/Glossary/String), a [number](/en-US/docs/Glossary/Number) or a [boolean](/en-US/docs/Glossary/Boolean). [Primitive](/en-US/docs/Glossary/Primitive) values cannot hold any [property](/en-US/docs/Glossary/Property/JavaScript).
## Message
```plain
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
{{jsxref("TypeError")}}.
## What went wrong?
In [strict mode](/en-US/docs/Web/JavaScript/Reference/Strict_mode), a {{jsxref("TypeError")}} is raised when attempting to
create a property on [primitive](/en-US/docs/Glossary/Primitive) value such
as a [symbol](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol), a [string](/en-US/docs/Glossary/String), a [number](/en-US/docs/Glossary/Number) or a [boolean](/en-US/docs/Glossary/Boolean). [Primitive](/en-US/docs/Glossary/Primitive) values cannot hold any [property](/en-US/docs/Glossary/Property/JavaScript).
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
```js example-bad
"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 [primitive](/en-US/docs/Glossary/Primitive) from being used in such places, or fix the issue by creating the object equivalent {{jsxref("Object")}}.
```js example-good
"use strict";
const foo = new String("my string");
foo.bar = {};
```
## See also
- [Strict mode](/en-US/docs/Web/JavaScript/Reference/Strict_mode)
- [primitive](/en-US/docs/Glossary/Primitive)