js undefined error All In One
Uncaught TypeError: Cannot read property 'key' of undefined
OK ✅
const num = 1;
// 1
num.key;
// undefined
(1).key;
// undefined
Error ❌
const u = undefined;
u.key;
// Uncaught TypeError: Cannot read property 'key' of undefined
demo
const arr = [1, 2, 3];
const uid = 2;
// no error ✅, but bug ❌
const index = arr.findIndex(item => item.id === uid);
// undefined value bug ❌
const arr = [undefined, 1, 2, 3];
const uid = 2;
// error ❌ & bug ❌
const index = arr.findIndex(item => item.id === uid);
Uncaught TypeError: Cannot read property 'id' of undefined