xxxxxxxxxx
if (typeof value !== 'undefined' && value) {
//deal with value'
};
xxxxxxxxxx
if( typeof myVar === 'undefined' || myVar === null ){
// myVar is undefined or null
}
xxxxxxxxxx
var myVar=null;
if(myVar === null){
//I am null;
}
if (typeof myVar === 'undefined'){
//myVar is undefined
}
xxxxxxxxxx
// simple check do the job
if (myString) {
// comes here either myString is not null,
// or myString is not undefined,
// or myString is not '' (empty).
}
xxxxxxxxxx
var myVar=null;
if(myVar === null){
//I am null;
}
if (typeof myVar === 'undefined'){
//myVar is undefined
}
xxxxxxxxxx
//check for null or undefined with nullish coalescing operator
let value = null ?? "Oops.. null or undefined";
console.log(value) //Oops.. null or undefined
value = 25 ?? "Oops.. null or undefined";
console.log(value) // 25
value = "" ?? "Oops.. null or undefined";
console.log(value) // ""
xxxxxxxxxx
Use for Empty string, undefined, null,
//To check for a truthy value:
if (strValue) {
// strValue was non-empty string, true, 42, Infinity, [], ...
}
//To check for a falsy value:
if (!strValue) {
// strValue was empty string, false, 0, null, undefined, ...
}
xxxxxxxxxx
// 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).
}