JavaScript: Block statement
Description
Block statements are commonly used with control flow statements i.e. while, for, if etc. The block is delimited by a pair of curly brackets.
Syntax
{ statement_1 statement_2 . . . statement_n }
Parameters
statement_1, statement_2, .... , statement_n
Where statements are grouped within the block statement.
Example:
if x>10
{
y=12
z=20
}
or while ( roll_no <= 40 )
{
roll_no++
}
JavaScript does not support block scope. In the following example output of the variable a will be 1 because var a statement within or before condition have same scope whereas in Java or C language the output of the same code will be 101.
var a = 101
{
a = 1;
}
console.log(a)
//output 1
Previous: JavaScript: void operator
Next: JavaScript: if...else statements
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics