Best practice of checking if variable is undefined (자바스크립트 변수 체크)

Words
38
Reading
1 min
Listen
Play
8y

javascript.png

 x === undefined

위와 같은 방법은 변수가 선언되어 있지 않으면 동작하지 않습니다.
아래와 같은 에러를 확인 할 수 있습니다.

 ReferenceError: x is not defined

위 방법은 변수가 선언되었지만, 정의가 되어있지 않을 때만 true를 return 해줍니다.

이 방법보다 더 좋은 방법은

 typeof x === 'undefined'

와 같은 방식으로 체크하는게 더 좋습니다.

Best practice of checking if variable is undefined (자바스크립트 변수 체크) | Ecency