TypeError: null/undefined has no properties

Ngoại lệ JavaScript "null (or undefined) has no properties" xảy ra khi bạn cố gắng truy cập các thuộc tính của null hoặc undefined. Chúng không có thuộc tính nào.

Thông báo

TypeError: Cannot read properties of undefined (reading 'x') (V8-based)
TypeError: Cannot destructure 'x' as it is undefined. (V8-based)
TypeError: Cannot destructure property 'x' of 'y' as it is undefined. (V8-based)
TypeError: null has no properties (Firefox)
TypeError: undefined has no properties (Firefox)
TypeError: undefined is not an object (evaluating 'undefined.x') (Safari)
TypeError: Right side of assignment cannot be destructured (Safari)

Loại lỗi

TypeError.

Nguyên nhân?

Cả nullundefined đều không có thuộc tính mà bạn có thể truy cập. Do đó, bạn không thể sử dụng property accessors trên chúng, hay destructure chúng.

Ví dụ

null và undefined không có thuộc tính

js
null.foo;
// TypeError: null has no properties

undefined.bar;
// TypeError: undefined has no properties

Xem thêm