block
Bir blok statement(ifade) (diğer bir deyişle birleşik ifade) sıfır ya da daha fazla ifadeyi gruplamak için kullanılır. Blok bir çift süslü parantezle sınırlandırılmakla beraber opsiyonel olarak etiketlenebilir
:
Syntax
[etiket:] { statement_1; statement_2; ... statement_n; }
statement_1
,statement_2
,statement_n
- İfadeler tek bir statement içinde sınırlandırılmıştır.
- etiket
- Görsel bir tanımlama yapmak ya da bir
break(kırılım)
oluşturmak için opsiyonel biretikettir.
.
Açıklama
Statement,sıklıkla akış tablolarıyla birlikte kullanılır. (örn. if...else
, for
, while
). Örnek:
while (x < 10) {
x++;
}
Hatırlatma : statement noktalı virgül ile sonlanmaz.
Block statement diğer dillerde birleşik ifade yani compound statement olarak ifade edilir. Javasacript tek bir statement beklerken, birden çok statement kullanmanızı sağlar. Blokların birleşmesi Javascript için yaygın bir kullanımdır. Bir diğer kullanım ise statement kullanmanız gereken ancak kullanmadığınız yerlerde boş bir statement kullanılmasıdır.
Block Scoping Rules
With var
Variables declared with var
do not have block scope. Variables introduced with a block are scoped to the containing function or script, and the effects of setting them persist beyond the block itself. In other words, block statements do not introduce a scope. Although "standalone" blocks are valid syntax, you do not want to use standalone blocks in JavaScript, because they don't do what you think they do, if you think they do anything like such blocks in C or Java. For example:
var x = 1;
{
var x = 2;
}
console.log(x); // logs 2
Burada çıktı 2 oldu çünkü block için ayrı bir scope oluşmamıştır. C ya da Java'da çıktı 1 olur.
let
ve const
ile
Yukarıdakinin aksine let
ve const
ayrı bir scope olarak koşacaktır:
let x = 1;
{
let x = 2;
}
console.log(x); // logs 1
x = 2
sınırlı bir statement içerisinde yer aldığından sadece o statement içerisinde geçerli olacaktır.
Bu kural const içinde aynıdır
:
const c = 1;
{
const c = 2;
}
console.log(c); // logs 1 and does not throw SyntaxError...
Dikkat edin sınırlı blocktaki const c = 2
ifadesi SyntaxError: Identifier 'c' zaten tanımlanmış
hatası vermedi çünkü, her blok içerisinde benzersiz olarak tanımlanır.
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript (ECMA-262) The definition of 'Block statement' in that specification. |
Living Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Block statement' in that specification. |
Standard | |
ECMAScript 5.1 (ECMA-262) The definition of 'Block statement' in that specification. |
Standard | |
ECMAScript 3rd Edition (ECMA-262) The definition of 'Block statement' in that specification. |
Standard | |
ECMAScript 1st Edition (ECMA-262) The definition of 'Block statement' in that specification. |
Standard | Initial definition. Implemented in JavaScript 1.0. |
Browser compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |