May 18, 2021 WeChat Mini Program Development Document
In WXS, you can use if statements in the following format:
Example syntax:
// if ...
if (表达式) 语句;
if (表达式)
语句;
if (表达式) {
代码块;
}
// if ... else
if (表达式) 语句;
else 语句;
if (表达式)
语句;
else
语句;
if (表达式) {
代码块;
} else {
代码块;
}
// if ... else if ... else ...
if (表达式) {
代码块;
} else if (表达式) {
代码块;
} else if (表达式) {
代码块;
} else {
代码块;
}
Example syntax:
switch (表达式) {
case 变量:
语句;
case 数字:
语句;
break;
case 字符串:
语句;
default:
语句;
}
Example 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
Example syntax:
for (语句; 语句; 语句)
语句;
for (语句; 语句; 语句) {
代码块;
}
Example code:
for (var i = 0; i < 3; ++i) {
console.log(i);
if( i >= 1) break;
}
Output:
0
1
Example syntax:
while (表达式)
语句;
while (表达式){
代码块;
}
do {
代码块;
} while (表达式)