不会就学-js

1. //??

// 左侧为空、false、null、undefined等falsy值时返回后面的值  
let arr = ['',0,-0,+0,NaN,false,undefined,null]
arr.forEach(item => console.log(item||'some other'))
some other
some other
some other
some other
some other
some other
some other
some other
?? 左侧为null或undefined时返回后面的值,否则返回前面的值
let arr = ['',0,-0,+0,NaN,false,undefined,null]
arr.forEach(item => console.log(item??'some other'))

0
-0
0
NaN
false
some other
some other

2. 数组合并

let a = [1, 2, 3]
let b = [4, 5, 6]

let c = a.concat(b)

for(let i in b) {
    a.push(b[i])
}

 a.push.apply(a,b) // apply第二个参数可为数组

let d = [...a,...b]

3. forEach提前退出

使用break会报错,使用return也不能跳出循环。

用 try···catch 捕获异常实现


try {
    let arr = [1,2,3,4,5]
    arr.forEach((item,index) => {
        if(item == 3) {
             // 一 代码错误跳出
            let err = xxx;

            // 二 抛出异常跳出
            // throw new Error('break'); 
        } else {
            console.log(item);
        }
    })
} catch (error) {
    if(error.message=='break') {
        console.log('提前结束循环啦');
    } else{
        console.log(error.message);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值