if 语句
let condition = 3;
// 结果输出 'if'
if(condition) console.log('if'); else console.log('else');let i = 3;
if (i > 25) console.log("Greater than 25."); // 只有一行代码的语句
else { console.log("Less than or equal to 25."); // 一个语句块
}//建议加上花括号
if (i > 25) {console.log("Greater than 25."); // 只有一行代码的语句
} else { console.log("Less than or equal to 25."); // 一个语句块
}if (i > 25) { console.log("Greater than 25.");
} else if (i < 0) { console.log("Less than 0.");
} else { console.log("Between 0 and 25, inclusive.");
}
do-while 语句
do-while
语句是一种后测试循环语句,即循环体中的代码执行后才会对退出条件进行求值。换句
话说,循环体内的代码至少执行一次。
let i = 0;
do { i += 2;
} while (i < 10);
// 在这个例子中,只要 i 小于 10,循环就会重复执行。i 从 0 开始,每次循环递增 2。
// 注意 后测试循环经常用于这种情形:循环体内代码在退出前至少要执行一次。
//运行结果i等于10
while 语句
while
语句是一种先测试循环语句,即先检测退出条件,再执行循环体内的代码。因此,
while
循
环体内的代码有可能不会执行。
let i = 0;
while (i < 10) { i += 2;
}
// 在这个例子中,变量 i 从 0 开始,每次循环递增 2。只要 i 小于 10,循环就会继续。
for 语句
for
语句也是先测试语句,只不过增加了进入循环之前的初始化代码,以及循环执行后要执行的表达式。
let count = 10;
for (let i = 0; i < count; i++) { console.log(i);
}
// 以上代码在循环开始前定义了变量 i 的初始值为 0。然后求值条件表达式,如果求值结果为 true
(i < count),则执行循环体。false则不执行循环体。如果循环体被执行了,则循环后表达式
也会执行,以便递增变量 i。for (;;) { // 无穷循环doSomething();
}// 如果只包含条件表达式,那么 for 循环实际上就变成了 while 循环:
let count = 10;
let i = 0;
for (; i < count; ) { console.log(i); i++;
}
for-in 语句
for-in 语句是一种严格的迭代语句,用于枚举对象中的非符号键属性
迭代(iterate),指的是按照某种顺序逐个访问列表中的每一项。比如,for语句。
ECMAScript
中对象的属性是无序的,因此
for-in
语句不能保证返回对象属性的顺序。换句话说,
所有可枚举的属性都会返回一次,但返回的顺序可能会因浏览器而异。
如果
for-in
循环要迭代的变量是
null
或
undefined
,则不执行循环体。
for (const propName in window) { document.write(propName);
}
// 这个例子使用 for-in 循环显示了 BOM 对象 window 的所有属性。每次执行循环,都会给变量
// propName 赋予一个 window 对象的属性作为值,直到 window 的所有属性都被枚举一遍。与 for 循环
// 一样,这里控制语句中的 const 也不是必需的。但为了确保这个局部变量不被修改,推荐使用 const。let obj = {};
obj.a =2;
obj.aa = 3;
obj.abc = 4;
for(const propName in obj){console.log(propName);
}
// 运行结果输出 a aa abc
for-of 语句
for-of 语句是一种严格的迭代语句,用于遍历可迭代对象的元素
for-of 循环会按照可迭代对象的 next()方法产生值的顺序迭代元素。
如果尝试迭代的变量不支持迭代,则 for-of 语句会抛出错误。
注意
ES2018
对
for-of
语句进行了扩展,增加了
for-await-of
循环,以支持生成期
约(
promise
)的异步可迭代对象。
for (const el of [2,4,6,8]) { console.log(el);
}
// 输出 2 4 6 8
break 和 continue 语句
break
和
continue
语句为执行循环代码提供了更严格的控制手段。其中,
break
语句用于退出循环,往下执行循环后的下一条语句。
而 continue 语句也用于退出当前循环,但只是退出当前循环并继续执行下一个循环(就是说会再次从循环顶部 开始执行)。
let num = 0;
for (let i = 1; i < 10; i++) { if (i % 5 == 0) { // 退出循环 break; } num++;
}
console.log(num); // 4let num = 0;
for (let i = 1; i < 10; i++) { if (i % 5 == 0) { // 退出当前循环 continue; } num++;
}
console.log(num); // 8
标签语句
break 和 continue 都可以与标签语句一起使用,返回代码中特定的位置。
// start 是一个自定义的标签
start: for (let i = 0; i < count; i++) { console.log(i);
}let num = 0;
// outermost 是一个自定义的标签
outermost: for (let i = 0; i < 10; i++) { for (let j = 0; j < 10; j++) { if (i == 5 && j == 5) { // 执行到这里会跳到outermost标签的for循环,并结束outermost标签处的整个for循环break outermost; } num++; }
}
console.log(num); // 55
let num = 0;
outermost: for (let i = 0; i < 10; i++) { for (let j = 0; j < 10; j++) { if (i == 5 && j == 5) { // 回到outermost标签处,继续循环continue outermost; } num++;}
}
console.log(num); // 95
with 语句
with 语句的用途是将代码作用域设置为特定的对象
with
语句的主要场景是针对一个对象反复操作,这时候将代码作用域设置为该对象能提供便利.
严格模式不允许使用 with 语句,否则会抛出错误。
由于 with 语句影响性能且难于调试其中的代码,通常不推荐在产品代码中使用 with
语句。
let qs = location.search.substring(1);
let hostName = location.hostname;
let urls = location.href;// 下面代码等价于(由于每一行都用到了 location 对象。如果使用 with 语句,就可以少写一些代码:)
with(location) { let qs = search.substring(1); let hostName = hostname; let urls = href;
}
这里,
with
语句用于连接
location
对象。这意味着在这个语句内部,每个变量首先会被认为是
一个局部变量。如果没有找到该局部变量,则会搜索
location
对象,看它是否有一个同名的属性。如
果有,则该变量会被求值为
location
对象的属性。
switch 语句
switch 语句是与 if 语句紧密相关的一种流控制语句.
switch (i) { case 25: console.log("25"); break; case 35: console.log("35"); break; case 45: console.log("45"); break; default: console.log("Other");
}
为避免不必要的条件判断,最好给每个条件后面都加上
break
语句。如果确实需要连续匹配几个
条件,那么推荐写个注释表明是故意忽略了
break
let i = 25;
switch (i) { case 25: /*跳过*/ case 35: console.log("25 or 35"); break; case 45: console.log("45"); break; default: console.log("Other");
}
//输出 25 or 35
switch ("hello world") { case "hello" + " world": console.log("Greeting was found."); break; case "goodbye": console.log("Closing was found."); break; default: console.log("Unexpected message was found.");
}let num = 25;
switch (true) { case num < 0: console.log("Less than 0."); break; case num >= 0 && num <= 10: console.log("Between 0 and 10."); break; case num > 10 && num <= 20: console.log("Between 10 and 20."); break; default: console.log("More than 20.");
}
函数
// 编写函数
function sayHi(name, message) { console.log("Hello " + name + ", " + message);
}
// 调用函数
sayHi('yaoxs','haha'); //Hello yaoxs, haha
sayHi("Nicholas", "how are you today?"); // Hello Nicholas, how are you today?
function sum(num1, num2) { return num1 + num2;
}
const result = sum(5, 10); // result 等于15//只要碰到 return 语句,函数就会立即停止执行并退出。
//因此,return 语句后面的代码不会被执行。
function sum(num1, num2) { return num1 + num2; console.log("Hello world"); // 不会执行
}
function diff(num1, num2) { if (num1 < num2) { return num2 - num1; } else { return num1 - num2; }
}