Javascript에서 null과 undefined 체크 방법

2019. 6. 17. 18:23서버 프로그래밍

다른 동료가 코드 리뷰 도중에 null 체크를 굳이 해야하는지, undefined만 체크하는 것으로 충분하지 않은지 의견을 내었다. 사실 이 둘은 다르게 넘어올 수있기 때문에 번거롭더라도 null과 undefined를 모두 체크하는 것 좋지만, 다수의 변수값을 한번에 체크할 때는 코드가 지저분해지는 문제가 있을 수 있다. 그래서 찾아보니 다음과 같은 아주 좋은 방법이 있다. 물론 0이 넘어오지 않는 경우에만 해당될 듯.

 

https://stackoverflow.com/questions/2559318/how-to-check-for-an-undefined-or-null-variable-in-javascript

 

How to check for an undefined or null variable in JavaScript?

We are frequently using the following code pattern in our JavaScript code if (typeof(some_variable) != 'undefined' && some_variable != null) { // Do something with some_variable } Is ...

stackoverflow.com