undefined in javascript code example

Example 1: javascript check for undefined

if (typeof myVariable === 'undefined'){
    //myVariable is undefined
}

Example 2: javascript if undefined

if(typeof comment === 'undefined') {

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

} else if(comment === null){

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

}

Example 3: javacript detect undefined

//detecting undefined object property
if (typeof person.last_name === "undefined") {
    console.log("person last_name is undefined");
}

//detecting undefined variable 
if (typeof myVar === "undefined") {
    console.log("myVar is undefined");
}

Example 4: javascript assignment operator if undefined

const variable2 = variable1  || 'new';