undefined vs null의 차이 #516
kwag93
started this conversation in
Today I Learned
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
JavaScript에서 undefined와 null은 모두** “값이 없음”**을 의미하지만,
의도와 발생 시점이 다르다.
선요약
undefined
값이 할당되지 않음
할당주체 ->자바스크립트 엔진
의미 -> 값이 아직 없음
null :
값이 없음을 명시적으로 표현
할당 주체 -> 개발자
의미 -> 의도적으로 비워둔 상태
자동 vs 명시적
비교 연산
-> 이를 통해 value != null 을 하면 한번에 두개를 거를 수도 있음.
함수 기본값
: 인자가 전달되지 않거나 undefined가 전달될 때 기본값이 사용된다.
TypeScript에서의 활용
undefined: 값이 아직 세팅되지 않음
null: 의도적으로 비워둔 값
undefined는 “아직 값이 없는 상태”,
null은 “비워둔 상태”로 구분해야 코드의 의도가 명확해진다.
?.과 ??는 “값이 존재하지 않음(nullish)” 상태를 구분하기 위해 만들어진 문법이다.
즉, undefined나 null일 때만 특별히 작동한다.
문법 : ?.
작동 조건 : 왼쪽 값이 null or undefined
반환 : undefined 반환
문법 : ??
작동 조건 : 왼쪽 값이 null or undefined
반환 : 오른쪽 값 반환
Beta Was this translation helpful? Give feedback.
All reactions