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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
---
title: const
slug: Web/JavaScript/Reference/Statements/const
translation_of: Web/JavaScript/Reference/Statements/const
---
<div>{{jsSidebar("Statements")}}</div>
<p>170 / 5000</p>
<h2 id="Çeviri_sonuçları">Çeviri sonuçları</h2>
<p>Sabit değerler(const), let anahtar sözcüğü kullanılarak bildirilen değişkenler gibi blok kapsamlıdır. Bir sabitin değeri yeniden atama yoluyla değiştirilemez ve yeniden beyan edilemez.</p>
<div>{{EmbedInteractiveExample("pages/js/statement-const.html")}}</div>
<div class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox notranslate">const <var>name1</var> = <var>value1</var> [, <var>name2</var> = <var>value2</var> [, ... [, <var>nameN</var> = <var>valueN</var>]]];</pre>
<dl>
<dt><code><var>nameN</var></code></dt>
<dd>The constant's name, which can be any legal {{Glossary("identifier")}}.</dd>
<dt><code><var>valueN</var></code></dt>
<dd>The constant's value. This can be any legal <a href="/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Expressions">expression</a>, including a function expression.</dd>
</dl>
<p> The <a href="/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">Destructuring Assignment </a>syntax can also be used to declare variables.</p>
<pre class="syntaxbox notranslate">const <var>{ bar }</var> = <em>foo</em>; // where foo = { bar:10, baz:12 };
/* This creates a constant with the name 'bar', which has a value of 10 */</pre>
<dl>
</dl>
<h2 id="Description">Description</h2>
<p>This declaration creates a constant whose scope can be either global or local to the block in which it is declared. Global constants do <strong>not</strong> become properties of the {{domxref("window")}} object, unlike {{jsxref("Statements/var", "var")}} variables.</p>
<p>An initializer for a constant is required. You must specify its value in the same statement in which it's declared. (This makes sense, given that it can't be changed later.)</p>
<p>The <strong><code>const</code> declaration</strong> creates a read-only reference to a value. It does <strong>not</strong> mean the value it holds is immutable—just that the variable identifier cannot be reassigned. For instance, in the case where the content is an object, this means the object's contents (e.g., its properties) can be altered.</p>
<p>All the considerations about the "<a href="/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone">temporal dead zone</a>" apply to both {{jsxref("Statements/let", "let")}} and <code>const</code>.</p>
<p>A constant cannot share its name with a function or a variable in the same scope.</p>
<h2 id="Examples">Examples</h2>
<h3 id="Basic_const_usage">Basic const usage</h3>
<p>Constants can be declared with uppercase or lowercase, but a common convention is to use all-uppercase letters.</p>
<pre class="brush: js; notranslate">// define MY_FAV as a constant and give it the value 7
const MY_FAV = 7;
// this will throw an error - Uncaught TypeError: Assignment to constant variable.
MY_FAV = 20;
// MY_FAV is 7
console.log('my favorite number is: ' + MY_FAV);
// trying to redeclare a constant throws an error
// Uncaught SyntaxError: Identifier 'MY_FAV' has already been declared
const MY_FAV = 20;
// the name MY_FAV is reserved for constant above, so this will fail too
var MY_FAV = 20;
// this throws an error too
let MY_FAV = 20;
</pre>
<h3 id="Block_scoping">Block scoping</h3>
<p>It's important to note the nature of block scoping.</p>
<pre class="brush: js notranslate">if (MY_FAV === 7) {
// this is fine and creates a block scoped MY_FAV variable
// (works equally well with let to declare a block scoped non const variable)
let MY_FAV = 20;
// MY_FAV is now 20
console.log('my favorite number is ' + MY_FAV);
// this gets hoisted into the global context and throws an error
var MY_FAV = 20;
}
// MY_FAV is still 7
console.log('my favorite number is ' + MY_FAV);
</pre>
<h3 id="const_needs_to_be_initialized">const needs to be initialized</h3>
<pre class="brush: js notranslate">// throws an error
// Uncaught SyntaxError: Missing initializer in const declaration
const FOO;
</pre>
<h3 id="const_in_objects_and_arrays">const in objects and arrays</h3>
<p>const also works on objects and arrays.</p>
<pre class="brush: js notranslate">const MY_OBJECT = {'key': 'value'};
// Attempting to overwrite the object throws an error
// Uncaught TypeError: Assignment to constant variable.
MY_OBJECT = {'OTHER_KEY': 'value'};
// However, object keys are not protected,
// so the following statement is executed without problem
MY_OBJECT.key = 'otherValue'; // Use Object.freeze() to make object immutable
// The same applies to arrays
const MY_ARRAY = [];
// It's possible to push items into the array
MY_ARRAY.push('A'); // ["A"]
// However, assigning a new array to the variable throws an error
// Uncaught TypeError: Assignment to constant variable.
MY_ARRAY = ['B'];</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ESDraft', '#sec-let-and-const-declarations', 'Let and Const Declarations')}}</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("javascript.statements.const")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li>{{jsxref("Statements/var", "var")}}</li>
<li>{{jsxref("Statements/let", "let")}}</li>
<li><a href="/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Constants">Constants in the JavaScript Guide</a></li>
</ul>
|