I. separate judgment
1 varA=NULL;2 //var a=undefined;3 //var a= ';4 //var a= ' DD ';5 if(!a&&typeofA = = ' object '){6Console.log (' A is null '))7}Else if(!a&&typeofA = = ' undefined '){8Console.log (' A is undefined ')9}Else if(!a&&typeofA = = ' string '){TenConsole.log (' A is an empty string ')) One}Else{ AConsole.log (' Other ') -}
Two. The project may also have null, undefined, empty strings, and other requirements for judging
1 //var a=null;2 //var a=undefined;3 //var a= ';4 varA= ' DD ';5 if(!a && (typeofA = = ' object ' | |typeofA = = ' undefined ' | |typeofA = = ' string ')){6Console.log (' A is null, undefined, one of the empty strings ')7}Else{8Console.log (' Other ')9}
Three. encapsulating a function for your own use is that better?
1 functionJudgedata (str) {2 if(!str && (typeofstr = = ' object ' | |typeofstr = = ' undefined ' | |typeofstr = = ' String ')){3 return false;4}Else{5 return true;6 }7 }8 //by returning false or true to determine if there is data9Console.log (Judgedata (' DD '));//trueTenConsole.log (Judgedata (NULL));//false OneConsole.log (Judgedata (undefined));//false AConsole.log (Judgedata ("));//false
javascript-the distinction between null, undefined, and empty strings