배열, 오브젝트에 존재하지 않는 키값으로 참조할때 if문

이태혁·2020년 9월 14일
0
  • 빈오브젝트에 키값 3이 없을 때 조건주기
let obj = {};

if (!obj[3]) {
  console.log('obj[3] is not existed')
}
if (typeof obj[3] === 'undefined') {
  console.log('obj[3] is not existed')
}
if (typeof(obj[3]) === 'undefined') {
  console.log('obj[3] is not existed')
}
  • 빈배열에 3번째 인덱스가 값이 없을 때 조건주기
let arr = [];

if (!arr[3]) {
  console.log('arr[3] is not existed')
}
if (typeof arr[3] === 'undefined') {
  console.log('arr[3] is not existed')
}
if (typeof(arr[3]) === 'undefined') {
  console.log('arr[3] is not existed')
}
profile
back-end, cloud, docker, web의 관심이 있는 예비개발자입니다.

0개의 댓글