tencent cloud

All product documents
Tencent Cloud Super App as a Service
Statements
Last updated: 2024-11-21 18:34:18
Statements
Last updated: 2024-11-21 18:34:18

if Statements

In WXS, you can use if statements in the following formats:
if (expression) statement : Execute statement when expression is truthy.
if (expression) statement1 else statement2 : Execute statement1 when expression is truthy. Otherwise, execute statement2.
if ... else if ... else statementN Using this statement, you can choose one of the executions from statement1 to statementN.
Example Syntax:
// 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 Statements

Example Syntax:
switch (Expression) {
case Variable:
Statement;
case Number:
Statement;
break;
case String:
Statement;
default:
Statement;
}
default branches can be omitted.
After the case keyword, you can only use a variable, number, or string.
Sample code:
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:
number 10

for Statements

Example Syntax:
for (Statement; Statement; Statement)
Statement;

for (Statement; Statement; Statement) {
Code block;
}
Supports the use of break and continue keywords.
Sample code:
for (var i = 0; i < 3; ++i) {
console.log(i);
if( i >= 1) break;
}
Output:
0
1

while Statements

Example Syntax:
while (Expression)
Statement;

while (Expression){
Code block;
}

do {
Code block;
} while (Expression)
When expression is true, loop execute statement or code block.
Supports the use of break and continue keywords.


Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 available.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon