js多条件判断的简写

Posted on 2021-01-21 10:13  猫头唔食鱼  阅读(1579)  评论(0编辑  收藏  举报
 // 非简写
        let x = 'a'
        if (x === 'a' || x === 'b' || x === 'c' || x === 'd') {
            console.log(11111);
        }

        // 简写
        let arr = ['a', 'b', 'c', 'd']
        if (arr.includes(x)) {
            console.log(11111);
        }


      // 如果是判断是不是空值,那就不用像上面那样了
    
        let y = null
        if (y==null || y ==undefined || y==''){
            console.log(2222);
        }
        // 要写成这样啊
        if(!y){
            console.log(2222);
        }
		
		
		// 封装成方法
        let includesContent = (arr,x) =>arr.includes(x)
        console.log(includesContent(arr,NaN)); // true 
        console.log(includesContent(arr,null));// true 
        console.log(includesContent(arr,undefined));// true 
        console.log(includesContent(arr,'a'));// true 
        console.log(includesContent(arr,66)); // false