--- title: 'TypeError: can''t access property "x" of "y"' slug: Web/JavaScript/Reference/Errors/Cant_access_property tags: - Erros - JavaScript - TypeError translation_of: Web/JavaScript/Reference/Errors/Cant_access_property ---
TypeError: Unable to get property {x} of undefined or null reference (Edge) TypeError: can't access property {x} of {y} (Firefox) TypeError: {y} is undefined, can't access property {x} of it (Firefox) TypeError: {y} is null, can't access property {x} of it (Firefox) Exemplos: TypeError: x is undefined, can't access property "prop" of it TypeError: x is null, can't access property "prop" of it TypeError: can't access property "prop" of undefined TypeError: can't access property "prop" of null
{{jsxref("TypeError")}}.
O acesso a propriedade foi realizado com um valor {{jsxref("undefined")}} ou {{jsxref("null")}}.
// casos undefined e null, onde o metódo substring não irá funcionar var foo = undefined; foo.substring(1); // TypeError: x is undefined, can't access property "substring" of it var foo = null; foo.substring(1); // TypeError: x is null, can't access property "substring" of it
Para corrigir o problema de valores undefined
ou null
, você pode usar o operador typeof, como no exemplo abaixo.
if (typeof foo !== 'undefined') { // Agora que sabemos que foo está definida, podemos prosseguir }