&& 논리연산자 문법

error-606·2025년 9월 1일

문법

목록 보기
3/3

&& 논리연산자

if (mod && typeof (mod as any).ReactComponent === 'function')

mod && ...

mod가 존재하는지 확인

mod가 falsy (undefined, null, 0, '')가 아닐 때

typeof (mod as any).ReactComponent === 'function'

mod 객체에 RacrComponent라는 프로퍼티가 있고, typeof(타입 확인하는)으로 그 타입이 'function'인지 확인 맞으면 true

예시)

const mod1 = { ReactComponent: () => {} }; // 함수
const mod2 = { ReactComponent: "hello" };  // 문자열
const mod3 = null;

if (mod1 && typeof mod1.ReactComponent === 'function') {
  console.log('mod1 실행됨'); // 실행
}
if (mod2 && typeof mod2.ReactComponent === 'function') {
  console.log('mod2 실행됨'); // 실행 안 됨
}
if (mod3 && typeof mod3.ReactComponent === 'function') {
  console.log('mod3 실행됨'); // 실행 안 됨
}
profile
프론트엔드 연습생

0개의 댓글