Q:

javascript check if undefined or null

if (typeof myVar !== "undefined") {
    console.log("myVar is DEFINED");
}
13
var myVar=null;

if(myVar === null){
    //I am null;
}

if (typeof myVar === 'undefined'){
    //myVar is undefined
}
2
if( typeof myVar === 'undefined' || myVar === null ){
    // myVar is undefined or null
}
0
Undefined  used for unintentionally missing values.

Null       used for intentionally missing values. 
3
// simple check do the job
if (myString) {
 // comes here either myString is not null,
 // or myString is not undefined,
 // or myString is not '' (empty).
}
4
if (typeof value !== 'undefined' && value) {
    //deal with value'
};
0
//check if string is undefined 
if (typeof myVar === 'undefined'){
  console.log("I am not defined");
}

//check if string is empty, null, or 0
var emptyString="";
if (emptyString) {
    console.log("im not empty");
}

0
// simple check do the job
if (myVar) {
 // comes here either myVar is not null,
 // or myVar is not undefined,
 // or myVar is not '' (empty string).
}
1

New to Communities?

Join the community