Message
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) Exemples 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
Types d'erreur
Quel est le problème ?
Exemples
Cas invalides
// undefined et null ne possèdent aucune propriété et aucune méthode substring
var toto = undefined;
toto.substring(1); // TypeError: x is undefined, can't access property "substring" of it
var toto = null;
toto.substring(1); // TypeError: x is null, can't access property "substring" of it
Corriger le problème
Pour détecter le cas où la valeur utilisée est undefined
ou null
, on peut utiliser l'opérateur typeof
. Par exemple :
if (typeof toto !== 'undefined') {
// On sait alors que toto est bien défini et on peut utiliser ses propriétés s'il en a.
}