Frontend practice/java script
JS 단락회로 평가
판다꼬마
2022. 5. 18. 21:32
728x90
-단락 회로 평가
피연산자 중에 뒤에 위치한 피연산자를 확인할 필요 없는 것
const getName = (person) => {
const name = person && person.name;
return name || "객체가 아닙니다.";
};
let person = { name: "추성준" };
let name = getName(person);
console.log(name);
person에 추성준이라는 객체가 저장되었다.
그러므로 return name|| "객체가 아닙니다.";
에서 name이 true이므로 뒤에는 확인을 하지 않고
name을 출력한다.
const getName = (person) => {
const name = person && person.name;
return name || "객체가 아닙니다.";
};
let person = null;
let name = getName(person);
console.log(name);
person에 null이 저장되었다.
person이 false이므로 const name=false이고
return name || "객체가 아닙니다.";
그러므로 객체가 아닙니다가 출력된다.
728x90