null, undefined 체크

Joah·2022년 10월 17일
0

Javascript

목록 보기
13/16

자바스크립트에서 null과 undefined 체크를 위해서는 동등 연산자가(==) 아닌 일치 연산자를(===) 사용해야 한다.

또는 NOT 연산자를 사용한다.

만약 null과 undefined 둘 다 걸러주기 위함이라면 동등 연산자(==)를 사용한다.

const a = undefined;
const typeofA = typeof(a); //undefined


const b = null
const typeofB = typeof(b); //object

console.log(a==b);
//true
console.log(a===b);
//false
console.log(typeofA === typeofB);
//false
  • 동등 연산자를 사용하여 null 과 undefined를 비교하면 true가 반환된다.

  • 일치 연산자를 사용하여 비교하면 false가 반환된다.


👩🏻‍⚖️ 따라서 값이 null과 undefined인지 정확히 체크하고 싶다면 일치 연산자를 사용해야 한다.



if(event.currentTarget == null){
	return event.currentTarget
}
  • 이때 동등 연산자를 사용한 이유는 null과 undefined를 동시에 걸러내기 위함이다.

사용하는 목적에 따라 동등 연사자를 사용할 수 있지만 반드시 알고 작성하자!

profile
Front-end Developer

0개의 댓글