Statement

if statement

In the .sjs file, it is possible to use the if statement in the following formats:

  • if (expression) statement: when expression is true, execute statement.
  • if (expression) statement1 else statement2: when expression is true, execute statement1. Otherwise, execute statement2
  • if ... else if ... else statementN This statement allows selecting one of statement1 ~ 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 only variable, 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 of statement or code block.
  • Supporting use of break and continue keywords.