From 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:43:23 -0500 Subject: initial commit --- .../reference/statements/const/index.html | 153 +++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 files/tr/web/javascript/reference/statements/const/index.html (limited to 'files/tr/web/javascript/reference/statements/const') diff --git a/files/tr/web/javascript/reference/statements/const/index.html b/files/tr/web/javascript/reference/statements/const/index.html new file mode 100644 index 0000000000..39f62e3144 --- /dev/null +++ b/files/tr/web/javascript/reference/statements/const/index.html @@ -0,0 +1,153 @@ +--- +title: const +slug: Web/JavaScript/Reference/Statements/const +translation_of: Web/JavaScript/Reference/Statements/const +--- +
{{jsSidebar("Statements")}}
+ +

170 / 5000

+ +

Çeviri sonuçları

+ +

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.

+ +
{{EmbedInteractiveExample("pages/js/statement-const.html")}}
+ + + +

Syntax

+ +
const name1 = value1 [, name2 = value2 [, ... [, nameN = valueN]]];
+ +
+
nameN
+
The constant's name, which can be any legal {{Glossary("identifier")}}.
+
valueN
+
The constant's value. This can be any legal expression, including a function expression.
+
+ +

 The Destructuring Assignment syntax can also be used to declare variables.

+ +
const { bar } = foo; // where foo = { bar:10, baz:12 };
+/* This creates a constant with the name 'bar', which has a value of 10 */
+ +
+
+ +

Description

+ +

This declaration creates a constant whose scope can be either global or local to the block in which it is declared. Global constants do not become properties of the {{domxref("window")}} object, unlike {{jsxref("Statements/var", "var")}} variables.

+ +

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.)

+ +

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 the case where the content is an object, this means the object's contents (e.g., its properties) can be altered.

+ +

All the considerations about the "temporal dead zone" apply to both {{jsxref("Statements/let", "let")}} and const.

+ +

A constant cannot share its name with a function or a variable in the same scope.

+ +

Examples

+ +

Basic const usage

+ +

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 - 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;
+
+
+ +

Block scoping

+ +

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)
+  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);
+
+ +

const needs to be initialized

+ +
// throws an error
+// Uncaught SyntaxError: Missing initializer in const declaration
+
+const FOO;
+
+ +

const in objects and arrays

+ +

const also works on objects and arrays.

+ +
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'];
+ +

Specifications

+ + + + + + + + + + + + +
Specification
{{SpecName('ESDraft', '#sec-let-and-const-declarations', 'Let and Const Declarations')}}
+ +

Browser compatibility

+ + + +

{{Compat("javascript.statements.const")}}

+ +

See also

+ + -- cgit v1.2.3-54-g00ecf