diff options
Diffstat (limited to 'files/uk/web/javascript/reference/statements/const/index.html')
-rw-r--r-- | files/uk/web/javascript/reference/statements/const/index.html | 232 |
1 files changed, 0 insertions, 232 deletions
diff --git a/files/uk/web/javascript/reference/statements/const/index.html b/files/uk/web/javascript/reference/statements/const/index.html deleted file mode 100644 index 18e898e6cb..0000000000 --- a/files/uk/web/javascript/reference/statements/const/index.html +++ /dev/null @@ -1,232 +0,0 @@ ---- -title: const -slug: Web/JavaScript/Reference/Statements/const -tags: - - константа -translation_of: Web/JavaScript/Reference/Statements/const ---- -<div>{{jsSidebar("Statements")}}</div> - -<p><strong>Оголошення <code>const</code> </strong> створює посилання на значення, доступне лише для читання. Що <strong>не</strong> гарантує незмінність значення, на котре вказує посилання, а лише той факт, що не можна повторно присвоїти будь-яке значення змінній з відповідним ім'ям.</p> - -<h2 id="Синтаксис">Синтаксис</h2> - -<pre class="syntaxbox">const <em>назваКонстантноїЗмінної1 = <em>значення1 [</em>, назваКонстантноїЗмінної<em>2</em> = <em>значення2 [</em>, ... [</em>, <em>назваКонстантноїЗмінноїN</em> = <em><em>значення</em>N]]]</em>;</pre> - -<dl> - <dt><code>значенняN</code></dt> - <dd>Назва константи, будь-який прийнятний {{Glossary("identifier")}} (ідентифікатор).</dd> - <dt><code>значенняN</code></dt> - <dd>Значення константи; будь-яки дозволений вираз (<a href="/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Expressions">expression</a>).</dd> -</dl> - -<h2 id="Description">Description</h2> - -<p>This declaration creates a constant that can be either global or local to the function in which it is declared. An initializer for a constant is required; that is, you must specify its value in the same statement in which it's declared (which makes sense, given that it can't be changed later).</p> - -<p>Constants are block-scoped, much like variables defined using the <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a></code> statement. The value of a constant cannot change through re-assignment, and it can't be redeclared.</p> - -<p>All the considerations about the "<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_dead_zone_and_errors_with_let">temporal dead zone</a>" that apply to <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let">let</a></code>, also apply to <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> - -<p>The following example demonstrates how constants behave. Try this in your browser console.</p> - -<pre class="brush:js">// NOTE: Constants can be declared with uppercase or lowercase, but a common -// convention is to use all-uppercase letters. - -// define MY_FAV as a constant and give it the value 7 -const MY_FAV = 7; - -// this will throw an error in Firefox and Chrome (but does not fail in Safari) -MY_FAV = 20; - -// will print 7 -console.log("my favorite number is: " + MY_FAV); - -// trying to redeclare a constant throws an error -const MY_FAV = 20; - -// the name MY_FAV is reserved for constant above, so this will also fail -var MY_FAV = 20; - -// this throws an error also -let MY_FAV = 20; - -// it's important to note the nature of block scoping -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) - const 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); - -// Assigning to A const variable is a syntax error -const A = 1; A = 2; - -// throws an error, missing initializer in const declaration -const FOO; - -// const also works on objects -const MY_OBJECT = {"key": "value"}; - -// Overwriting the object behaves as above (throws an error in Firefox and Chrome but does not fail in Safari) -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 -</pre> - -<h2 id="Specifications">Specifications</h2> - -<table class="standard-table"> - <tbody> - <tr> - <th scope="col">Specification</th> - <th scope="col">Status</th> - <th scope="col">Comment</th> - </tr> - <tr> - <td>{{SpecName('ES6', '#sec-let-and-const-declarations', 'Let and Const Declarations')}}</td> - <td>{{Spec2('ES6')}}</td> - <td>Initial definition.</td> - </tr> - <tr> - <td>{{SpecName('ESDraft', '#sec-let-and-const-declarations', 'Let and Const Declarations')}}</td> - <td>{{Spec2('ESDraft')}}</td> - <td> </td> - </tr> - </tbody> -</table> - -<h2 id="Browser_compatibility">Browser compatibility</h2> - -<p>{{CompatibilityTable}}</p> - -<div id="compat-desktop"> -<table class="compat-table"> - <tbody> - <tr> - <th>Feature</th> - <th>Chrome</th> - <th>Edge</th> - <th>Firefox (Gecko)</th> - <th>Internet Explorer</th> - <th>Opera</th> - <th>Safari</th> - </tr> - <tr> - <td>Basic support</td> - <td>{{CompatChrome(21)}}</td> - <td>{{CompatVersionUnknown}}</td> - <td>{{CompatGeckoDesktop(36)}}</td> - <td>11</td> - <td>12</td> - <td>5.1</td> - </tr> - <tr> - <td>Reassignment fails</td> - <td>{{CompatChrome(20)}}</td> - <td>{{CompatVersionUnknown}}</td> - <td>{{CompatGeckoDesktop(13)}}</td> - <td>11</td> - <td>{{CompatUnknown}}</td> - <td>{{CompatUnknown}}</td> - </tr> - <tr> - <td>Allowed in sloppy mode</td> - <td>{{CompatChrome(49.0)}}</td> - <td> </td> - <td> </td> - <td> </td> - <td> </td> - <td> </td> - </tr> - </tbody> -</table> -</div> - -<div id="compat-mobile"> -<table class="compat-table"> - <tbody> - <tr> - <th>Feature</th> - <th>Android</th> - <th>Android Webview</th> - <th>Firefox Mobile (Gecko)</th> - <th>IE Mobile</th> - <th>Opera Mobile</th> - <th>Safari Mobile</th> - <th>Chrome for Android</th> - </tr> - <tr> - <td>Basic support</td> - <td>{{CompatNo}}</td> - <td>{{CompatVersionUnknown}}</td> - <td>{{CompatUnknown}}</td> - <td>{{CompatUnknown}}</td> - <td>{{CompatVersionUnknown}}</td> - <td>{{CompatUnknown}}</td> - <td>{{CompatVersionUnknown}}</td> - </tr> - <tr> - <td>Reassignment fails</td> - <td>{{CompatNo}}</td> - <td>{{CompatVersionUnknown}}</td> - <td>{{CompatUnknown}}</td> - <td>{{CompatUnknown}}</td> - <td>{{CompatVersionUnknown}}</td> - <td>{{CompatUnknown}}</td> - <td>{{CompatVersionUnknown}}</td> - </tr> - <tr> - <td>Allowed in sloppy mode</td> - <td>{{CompatNo}}</td> - <td>{{CompatChrome(49.0)}}</td> - <td> </td> - <td> </td> - <td> </td> - <td> </td> - <td>{{CompatChrome(49.0)}}</td> - </tr> - </tbody> -</table> -</div> - -<h2 id="Compatibility_notes">Compatibility notes</h2> - -<p>In earlier versions of Firefox & Chrome and as of Safari 5.1.7 and Opera 12.00, if you define a variable with <code>const</code>, you can still change its value later. It is not supported in Internet Explorer 6-10, but is included in Internet Explorer 11.</p> - -<h3 id="Firefox-specific_notes">Firefox-specific notes</h3> - -<p>The <code>const</code> declaration was implemented in Firefox long before <code>const</code> appeared in the ECMAScript 2015 (ES6) specification. For <code>const</code> ES6 compliance see {{bug(950547)}} and {{bug(611388)}}.</p> - -<ul> - <li>Prior to SpiderMonkey 46 {{geckoRelease(46)}}, a {{jsxref("TypeError")}} was thrown on redeclaration instead of a {{jsxref("SyntaxError")}} ({{bug(1198833)}}).</li> - <li>Starting with SpiderMonkey 36 {{geckoRelease("36")}}: - <ul> - <li><code>{const a=1};a</code> now throws a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError" title="The ReferenceError object represents an error when a non-existent variable is referenced."><code>ReferenceError</code></a> and does not return <code>1</code> anymore due to block-scoping.</li> - <li><code>const a;</code> now throws a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError" title="The SyntaxError object represents an error when trying to interpret syntactically invalid code."><code>SyntaxError</code></a> ("missing = in const declaration<code>"</code>): An initializer is required.</li> - <li><code>const a = 1; a = 2;</code> now also throws a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError" title="The SyntaxError object represents an error when trying to interpret syntactically invalid code."><code>SyntaxError</code></a> ("invalid assignment to const a").</li> - </ul> - </li> -</ul> - -<h2 id="See_also">See also</h2> - -<ul> - <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/var"><code>var</code></a></li> - <li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/let"><code>let</code></a></li> - <li><a href="/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#Constants">Constants in the JavaScript Guide</a></li> -</ul> |