xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js undefined error All In One

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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

refs

TypeError

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2021-07-07 07:12  xgqfrms  阅读(51)  评论(0编辑  收藏  举报