Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

block

Summary

Block digunakan untuk mengelompokkan 0 atau lebih statement. Block dibuka dan ditutup oleh sepasang { }.

Version Information

Statement
Implemented in: JavaScript 1.0, NES 2.0
ECMA Version: ECMA-262

Syntax

{
   statement_1
   statement_2
   ...
   statement_n
}

Parameters

statement_1, statement_2, statement_n
Statements grouped within the block statement.

Description

This statement is commonly used with control flow statements (e.g. if, for, while). For example:

while (x < 10) {
   x++;
}

Important: JavaScript does 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;
}
alert(x); // outputs 2

This outputs 2 because the var x statement within the conditional is in the same scope as the var x statement before the conditional. In C or Java, the equivalent code would have outputted 1.

Tag Dokumen dan Kontributor

 Kontributor untuk laman ini: teoli, keripix
 Terakhir diperbarui oleh: teoli,