Snippet

JavaScript vérifie si non défini

6 réponses de la communauté

if (typeof myVar !== "undefined") {
    console.log("myVar is DEFINED");
}
if (typeof myVariable === 'undefined'){
    //myVariable is undefined
}
if(typeof comment === 'undefined') {

  alert('Variable "comment" is undefined.');

} else if(comment === null){

  alert('Variable "comment" is null.');

}
let id;

if(typeof id === 'undefined') {
    console.log("id is undefined...");
}
if (typeof myFlag !== "undefined") {
  // comes here whether myFlag is true or false but not defined
}
if (typeof myVariable === 'undefined'){
    //myVariable is undefined
}