Javascript学习
二、运算符与流程控制
运算符
赋值运算符
算术运算符
复合运算符
一元运算符
比较运算符
逻辑运算符
优先级:&& 优先级高于 ||
true || false && false //true
流程控制
if
if/else
三元表达式
switch
while
do/while
for
break/continue
label
for/in
用于遍历对象的所有属性,for/in主要用于遍历对象,不建议用来遍历数组
let obj = {
name: "xx",
age: 18
}
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
console.log(obj[key]);
}
}
for/of
用来遍历数组,strings, maps, sets等可迭代的数据结构
注意:与for/in不同的是 for/of每次循环取其中的值而不是索引
遍历数组:
let arr = [1, 2, 3];
for( const iterator of arr) {
console.log(iterator); // 1 2 3
}
遍历字符串:
let arr = 'abc';
for( const iterator of arr) {
console.log(iterator); // a b c
}
使用迭代特性遍历数组:
let arr = ['a', 'b'];
for( const [key, value] of arr.entries()) {
console.log(key, value);
// 0 'a'
// 1 'b'
}