javascript Control flow(控制语句)

Conditional statements(控制语句)

if…else

if (condition) {
  statement_1;
} else {
  statement_2;
}
if (condition_1) {
  statement_1;
} else if (condition_2) {
  statement_2;
} else if (condition_n) {
  statement_n;
} else {
  statement_last;
} 

1.判断条件为false false; undefined; null; 0; NaN; the empty string (""). 其余都为true

switch statement

switch (expression) {
  case label_1:
    statements_1
    [break;]
  case label_2:
    statements_2
    [break;]default:
    statements_def
    [break;]
}

Loops and iteration

for statement

for ([initialExpression]; [condition]; [incrementExpression])
  statement

for…in… 输出的是index

for (variable in object)
  statement

for…of…输出的是value

for (variable of object)
  statement
const arr = [3, 5, 7];
arr.foo = 'hello';

for (let i in arr) {
   console.log(i); // logs "0", "1", "2", "foo"
}

for (let i of arr) {
   console.log(i); // logs 3, 5, 7
}

do…while statement; while statement

do
  statement
while (condition);
while (condition)
  statement

break continue

参考:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Control_flow_and_error_handling
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration

发布了1794 篇原创文章 · 获赞 582 · 访问量 154万+

猜你喜欢

转载自blog.csdn.net/claroja/article/details/104310764
今日推荐