[javascript] 기초

bin·2023년 1월 9일
0

console.log(!undefined);
console.log(!null);
console.log(!0);
console.log(!"");
console.log(!NaN);
console.log(!-0);

위의 것들은 원래 true인데 ! 연산자를 이용해서 true값이 나오게끔 했다.

내가 왜 위의 있는 코드를 가져왔냐면 저 코드들을 제외한 모든 코드는 true이기 때문이기도 하고 못 풀었던 문제에서 나오는 친구들이라 가져왔다.

문제는 다음과 같다,

function meetAt(year, month, date) {
  if(year && !month && !date) {
    return year + "년"
  }else if (year && !month){
    return year + "년" + month + "월"
  } else if (year && month && date) {
    return year + "/" + month + "/" + date;
  }
}

meetAt(2020, 01, 20);

입력한 날짜값에따라 다르게 출력되는데 여기서 ! not 연산자가 유용하게 사용된다.

profile
프론트엔드부터 공부하고 있습니다

0개의 댓글