인프런 커뮤니티 질문&답변
작성자 없음
작성자 정보가 삭제된 글입니다.
counter가 없는데, null 이 아니라 0으로 반환
작성
·
240
1
local storage에 counter를 아에 삭제하고 새로고침 했을때
counter가 null 이 아니라 0이라서 counter === null 조건에 걸리지 않는걸까요?
counter가 null 이 아니라 0이라서 counter === null 조건에 걸리지 않는걸까요?
console로 찍어보면
console.log(jsonLocalStorage.getItem('counter'))
console.log(counter)
console.log(counter)
각각 null, 0이 찍힙니다.
0과 null이 같다고 생각하면 화면상에 0번째가 찍히지 않아야 하는데..
if (Number(jsonLocalStorage.getItem('counter')) === 0) {
console.log("0 check")
}
if (Number(jsonLocalStorage.getItem('counter')) === 0) {
console.log("0 check")
}
if (Number(jsonLocalStorage.getItem('counter')) === null) {
console.log("null check")
}
0 check은 찍히고 null check은 찍히지 않습니다.
Number로 형변환을 하면 다르게 되는걸까요..
Number로 형변환을 하면 다르게 되는걸까요..






