-
[JavaScript] Number + (undefined, null, false)와 NaNJavaScript 2023. 4. 25. 21:07
reduce를 쓰다가 특정 조건이면 falsy한 값이 되도록 falsy 한 값들을 더해보았는데 false와 null을 더하니 자바스크립트에서 임의로 0으로 평가되어 1이 나오고 undefined는 NaN이 나왔다. 아 그래서 undefined를 더하면 되겠다. 하고서 나온 결과값으로 if문을 사용하였는데 뭔가 찝찝해서 NaN에 대해서 살펴보니 NaN === false와 NaN === true가 false이고 타입은 숫자형이었다. 찾아보니 NaN은 isNaN() 메소드를 사용하여야 했다. 뭔가 알수록 굉장히 혼란스러웠다. 이렇게 사용하는 방식은 혼란을 줄 수 있어 앞으로는 NaN이 나오는 결과값에 대해서는 피하는 게 좋다고 생각하였다.
반응형'JavaScript' 카테고리의 다른 글
[JavaScript] 조합 알고리즘 코드 이해하기 (0) 2023.05.17 [JavaScript] 여러 request 처리하기 with RTK Query (0) 2023.05.08 [Javascipt] Clipboard API 사용 (0) 2023.03.04 [JavaScript] 배열 숫자 채우기 (0) 2023.02.10 [JavaScript] RTK Query: Automated Re-fetching (1) 2023.01.06