&&연산자

김정준·2022년 5월 22일
0

JS

목록 보기
4/13

&& 연산자는 true or false를 반환하는 게 아니다.
공식문서에 의하면

the operator returns the value of the first falsy operand encountered when evaluating from left to right, or the value of the last operand if they are all truthy.

result = '' && 'foo';   
// 좌에서 우로 evaluate 할 때 첫번째 falsy값인 ''를 만남 
// 따라서 result = '' 

result = 2 && 0;
// 좌에서 우로 evaluate 할 때 첫번째 falsy값인 0를 만남
// 따라서 result = 0

result = 'foo' && 4;   
// 좌에서 우로 evaluate 했는데 모두 truty값이다. 마지막 truthy 값이 4
// 따라서 result = 4

function App(){
  const name = "뤼액트";
  return <div>{name === '리액트' && <h1>리액트입니다</h1>}</div>;
}
// 여기서 name === '리액트' 는 false이므로 false가 반환된다.

0개의 댓글