天天看點

js undefined error All In One

js undefined error All In One

js undefined error All In One

Uncaught TypeError: Cannot read property 'key' of undefined
js undefined error All In One

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
js undefined error All In One

​​