check whether a variable is undefined javascript code example

Example 1: javascript if undefined

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

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

} else if(comment === null){

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

}

Example 2: js check if undefined

let foo = undefined
//will return true
typeof foo === 'undefined'

Example 3: nodejs check if variable is undefined

if ( typeof query !== 'undefined' && query )
{
  //do stuff if query is defined and not null
}
else
{

}

Example 4: javascript check undefined

if(myVar === null) {
	console.log("Element is undefined");
}

Example 5: undefined javascript check

if(undefinedElement === null) {
	console.log("Element is undefined");
}