다양한 조건에 논리 연산자

반응형

중첩된 if..else 또는 대소문자 전환을 줄이려면 기본 논리 연산자를 사용하면 됩니다 AND/OR.

function doSomething(arg1){ 
    arg1 = arg1 || 10; 
// set arg1 to 10 as a default if it’s not already set
return arg1;
}

let foo = 10;  
foo === 10 && doSomething(); 
// is the same thing as if (foo == 10) then doSomething();
// Output: 10

foo === 5 || doSomething();
// is the same thing as if (foo != 5) then doSomething();
// Output: 10
 
반응형