일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- Typescript Handbook
- 프론트 이미지 업로드
- 인터페이스 타입 비교
- Blob?
- Typescript Study
- ts.config.json default setting
- 공부
- ts 설정값
- Null vs Undefined
- Blob 청크
- 타입스크립트 필수 설정
- ts 프로젝트 설정 추천
- ts 설정
- TypeScript config
- firebase server
- 인터페이스 vs 타입
- 타입스크립트 설정값
- ts 필수 설정
- Blob이란?
- JS Blob
- Blob 사용하는 이유
- JS 이미지 미리보기
- Interface vs Type Alias
- 이미지 서버에 업로드
- why use interface?
- TypeScript
- ts.config.json
- TypeScript Undefined Null
- TypeScript Deep Dive
- ts 기초 설정
- Today
- Total
RPG처럼 웹 개발하기
Null vs Undefind 본문
용어설명
1. undefiend
- 어떠한 값도 할당되지 않아 자료형이 정해지지 않은 상태입니다.
2. null
- 자료형이 정해진(defined) 상태입니다.
- null 값이 할당된 상태입니다.
Null 과 Undefined 코드로 비교하기
console.log(null == null); // true
console.log(undefined == undefined); // true
console.log(null == undefined); // true
console.log(null === null); // true
console.log(undefined === undefined); // true
console.log(null === undefined); // false
null == undefined 결과가 true이기 때문에 작성 가능한 코드
const foo = (arg: string | null | undefined) => {
if (arg == null) {
// null 또는 undefined 값이 매개변수로 들어왔습니다.
} else {
console.log(arg);
}
}
Null 과 Undefined 어떤걸 사용해야 하지?
Typescript 공식 문서를 보면 아래와 같이 나와 있습니다.
undefined 를 사용하고 null 을 사용하지 마세요.
null 을 사용하지말고 undefined 를 사용하라고 하는 문서
Coding guidelines · microsoft/TypeScript Wiki
GitHub - microsoft/TypeScript: TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output. - GitHub - microsoft/TypeScript: TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
github.com
null 은 나쁜 아이디어!
알고 있으면 좋은 점 ( 근데 주관적인 생각도 좀 들어간… )
undefined 를 명시적으로 선언해주는것은 좋지 않습니다.
undefined는 변수는 존재하나, 어떠한 값도 할당되지 않은 상태라는 말 입니다.
의도적으로 undefined 를 선언해서 사용하는것은 옳지 않습니다.
의도적으로 undefined 값을 주는것 이라면 차라리 null 을 주는것이 낫다고 생각합니다.
하지만 더 좋은 방법은 그냥 값을 주지 않는것이죠.
어차피 참조하려고 하면 undefined를 자동으로 뱉을테니까요.
const badItem = { a: 1, b: undefined }; // bad
const goodItem = { a: 1 }; // good
참조 페이지 :
https://basarat.gitbook.io/typescript/
README - TypeScript Deep Dive
Share URL: https://basarat.gitbook.io/typescript/
basarat.gitbook.io
'웹 개발' 카테고리의 다른 글
TypeScript 설정에 필수로 작성 해야 하는것 (0) | 2022.12.16 |
---|---|
Interface VS Type alias 비교하기 (0) | 2022.11.20 |
Blob으로 이미지 업로드 훑어보기 (0) | 2022.10.02 |
PWA 푸시알림 어떻게 구현하지? (0) | 2022.09.05 |
시멘틱 태그( Sematic Tag )를 잘 사용하는 방법? (0) | 2022.08.24 |