I am familiar with one line if statement
, i found it here and here:
if (x==0) alert('zero');
Is it correct to use for loop
one line:
for (var i=0; i < 3; i++) alert(i);
this fiddle works just fine.
I am familiar with one line if statement
, i found it here and here:
if (x==0) alert('zero');
Is it correct to use for loop
one line:
for (var i=0; i < 3; i++) alert(i);
this fiddle works just fine.
Both methods are valid in Javascript.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Statements
All Javascript cares about is what is immediately after the for
statement. It can be a statement block (multiple statements in curly brackets) or a single statement.
This is true for nearly every control statement in Javascript.
Yes, it is correct to only have one statement there. In fact, it is required by the language. A for statement has the syntax:
for (ExpressionNoIn ; Expression ; Expression) Statement
notice that it only includes only one Statement
.
A block
is a type of statement which is defined using curly brackets and contains a StatementList
, so you can use a block for that statement, which is what you see when there are curly brackets.
You can also use any other statement there; it doesn't have to be a block
statement.