초보 개발자의 일기

JS 단락회로 평가 본문

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

'Frontend practice > java script' 카테고리의 다른 글

JS await,async  (0) 2022.05.19
JS spread, 비 구조화 할당  (0) 2022.05.19
JS Truthy ,Falsy  (0) 2022.05.17
js 배열내장 함수  (0) 2022.05.17
JS for 문  (1) 2022.05.15