-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
116 lines (81 loc) · 2.89 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
---
title: 'TypeError: invalid assignment to const "x"'
slug: Web/JavaScript/Reference/Errors/Invalid_const_assignment
page-type: javascript-error
---
{{jsSidebar("Errors")}}
The JavaScript exception "invalid assignment to const" occurs when it was attempted to
alter a constant value. JavaScript
[`const`](/en-US/docs/Web/JavaScript/Reference/Statements/const)
declarations can't be re-assigned or redeclared.
## Message
```plain
TypeError: Assignment to constant variable. (V8-based)
TypeError: invalid assignment to const 'x' (Firefox)
TypeError: Attempted to assign to readonly property. (Safari)
```
## Error type
{{jsxref("TypeError")}}
## What went wrong?
A constant is a value that cannot be altered by the program during normal execution. It
cannot change through re-assignment, and it can't be redeclared. In JavaScript,
constants are declared using the
[`const`](/en-US/docs/Web/JavaScript/Reference/Statements/const)
keyword.
## Examples
### Invalid redeclaration
Assigning a value to the same constant name in the same block-scope will throw.
```js example-bad
const COLUMNS = 80;
// …
COLUMNS = 120; // TypeError: invalid assignment to const `COLUMNS'
```
### Fixing the error
There are multiple options to fix this error. Check what was intended to be achieved
with the constant in question.
#### Rename
If you meant to declare another constant, pick another name and re-name. This constant
name is already taken in this scope.
```js example-good
const COLUMNS = 80;
const WIDE_COLUMNS = 120;
```
#### const, let or var?
Do not use const if you weren't meaning to declare a constant. Maybe you meant to
declare a block-scoped variable with
[`let`](/en-US/docs/Web/JavaScript/Reference/Statements/let) or
global variable with
[`var`](/en-US/docs/Web/JavaScript/Reference/Statements/var).
```js example-good
let columns = 80;
// …
columns = 120;
```
#### Scoping
Check if you are in the correct scope. Should this constant appear in this scope or was
it meant to appear in a function, for example?
```js example-good
const COLUMNS = 80;
function setupBigScreenEnvironment() {
const COLUMNS = 120;
}
```
### const and immutability
The `const` declaration creates a read-only reference to a value. It does
**not** mean the value it holds is immutable, just that the variable
identifier cannot be reassigned. For instance, in case the content is an object, this
means the object itself can still be altered. This means that you can't mutate the value
stored in a variable:
```js example-bad
const obj = { foo: "bar" };
obj = { foo: "baz" }; // TypeError: invalid assignment to const `obj'
```
But you can mutate the properties in a variable:
```js example-good
obj.foo = "baz";
obj; // { foo: "baz" }
```
## See also
- [`const`](/en-US/docs/Web/JavaScript/Reference/Statements/const)
- [`let`](/en-US/docs/Web/JavaScript/Reference/Statements/let)
- [`var`](/en-US/docs/Web/JavaScript/Reference/Statements/var)