&& 와 || 같은 논리 연산자에서
뒤에 를 볼 필요도 없는 경우 앞의 것만 보고 판단하게 되는데 이를 단락 회로 평가라고 한다.
true || false // 뒤를 볼 것도 없이 true이므로 뒤의 false는 무시됨
false && true // 뒤를 볼 것도 없이 false이므로 뒤의 true는 무시됨
const getName = (person) => {
const name = person && person.name; //( 이미 false이므로 person.name은 무시됨)
return name || "객체가 아닙니다" ; // 객체가 아닙니다
};
let person;
const name = getName(person);
console.log(name);
const getName = (person) => {
const name = person && person.name; //( 앞에 true으로 뒤의 true도 봐짐.)
return name || "객체가 아닙니다" ; // 김마요 ( 앞이 true이므로 뒤의 것은 무시됨)
};
let person = { name: "김마요"};
const name = getName(person);
console.log(name);
'TIL' 카테고리의 다른 글
브라우저별 스타일 잠깐 멈촤! RESET CSS / Normalize (2) | 2022.09.08 |
---|---|
서로가 믿을 수 있게! TCP 3 way handshake (2) | 2022.09.06 |
[css] width 속성의 초기값은 auto, 그 의미는? (0) | 2022.09.03 |
React '첫인상'과 JSX / state / Props (0) | 2022.08.18 |
배열 내장함수 11가지 ( forEach map includes indexOf findIndex find filiter slice concat sort join ) (0) | 2022.08.10 |