Statement
if statement
In the .sjs file, it is possible to use the if
statement in the following formats:
if (expression) statement
: whenexpression
is true, executestatement
.
if (expression) statement1 else statement2
: whenexpression
is true, executestatement1
. Otherwise, executestatement2
if ... else if ... else statementN
This statement allows selecting one ofstatement1
~statementN
for execution.
Sample syntax:
copy
// if ...
if (expression) statement;
if (expression)
statement;
if (expression) {
Code block;
}
// if ... else
if (expression) statement;
else statement;
if (expression)
statement;
else
statement;
if (expression) {
Code block;
} else {
Code block;
}
// if ... else if ... else ...
if (expression) {
Code block;
} else if (expression) {
Code block;
} else if (expression) {
Code block;
} else {
Code block;
}
switch statement
Sample syntax:
copy
switch (expression) {
case variable:
statement;
case number:
statement;
break;
case string:
statement;
default:
statement;
}
default
branches can be omitted.
- Behind
case
keyword, use onlyvariable
,number
,string
.
Sample codes:
copy
var exp = 10;
switch ( exp ) {
case "10":
console.log("string 10");
break;
case 10:
console.log("number 10");
break;
case exp:
console.log("var exp");
break;
default:
console.log("default");
}
Output:
copy
number 10
for statement
copy
Sample syntax:
for (statement; statement; statement)
statement;
for (statement; statement; statement) {
Code block;
}
- Supporting use of break and continue keywords.
Sample codes:
copy
for (var i = 0; i < 3; ++i) {
console.log(i);
if( i >= 1) break;
}
Output:
copy
0
1
while statement
copy
Sample syntax:
while (expression)
statement;
while (expression){
Code block;
}
do {
Code block;
} while (expression)
- When
expression
is true, do loop execution ofstatement
orcode block
.
- Supporting use of
break
andcontinue
keywords.