当前位置:网站首页>[js] skill simplification if empty judgment

[js] skill simplification if empty judgment

2022-07-05 16:28:00 533_


1. Simplify the conditional expression

//  Too long logical expression 
if (x === 'abc' || x === 'def' || x === 'ghi' || x ==='jkl') {
    
    // Other logic 
}

//  Abbreviation 
if (['abc', 'def', 'ghi', 'jkl'].includes(x)) {
    
   // Other logic 
}

simplify if … else

//  Novice writing 
let test= boolean;
if (x > 100) {
    
    test = true;
} else {
    
    test = false;
}

//  Shorthand expression 
let test = (x > 10) ? true : false;

//  More directly 
let test = x > 10;
console.log(test);

Leave blank and set the default value

if (first !== null || first !== undefined || first !== '') {
    
    let second = first;
}


//  Abbreviation 
let second = first || '';

For so many years JavaScript , I don't know these skills yet ?

原网站

版权声明
本文为[533_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207051549481757.html