
let or = true || false; // 두개의 값 중에 하나만 참이면 참 반환
let and = true && false; // 두개의 값이 모두 참이여야 참 반환
let not = !true; // 참이라면 거짓 , 거짓이라면 참 반환
첫번째 피연산자 값으로 연산의 결과를 확정할 수 있다면, 두번째 피연산자의 값에는 접근하지 않는 자바스크립트의 특징
function returnFalse(){
console.log("False 함수");
return false;
};
function returnTrue(){
console.log("True 함수");
return true;
};
console.log(returnFalse() && returnTrue());
단락평가로 인해 returnTrue()는 실행되지 않는다.
ex1)
function printName(person) {
console.log(person && person.name);
}
printName();
- person이 undefined 이라면 단락평가에 의해 person.name에는 접근하지 않음.
- 콘솔에 오류 없이 undefined이 출력됨
ex2)
function printName2(person) {
const name = person && person.name;
console.log(name || "person의 값이 없음");
}
printName2({name: "아무개"});