TypeError: Cannot read property 'length' of undefined
时间: 2023-10-16 07:23:18 浏览: 163
处理JavaScript值为undefined的7个小技巧
This error occurs when you try to access the length property of a variable that is undefined.
For example:
```
let arr;
console.log(arr.length); // TypeError: Cannot read property 'length' of undefined
```
In this case, the variable `arr` is undefined and does not have a length property. To fix this error, you can initialize the variable with an empty array or check if the variable is defined before accessing its length property.
```
let arr = [];
console.log(arr.length); // 0
if (arr) {
console.log(arr.length); // 0
}
```
阅读全文