xxxxxxxxxx
function isNullOrEmpty(str) {
return str === null || str.trim() === '';
}
// Usage example
const myString = null;
const result = isNullOrEmpty(myString);
console.log(result); // Output: true
xxxxxxxxxx
let myStr = null;
if (myStr === null || myStr.trim() === "") {
console.log("This is an empty string!");
} else {
console.log("This is not an empty string!");
}
/*
This will return:
"This is an empty string!"
*/
xxxxxxxxxx
// Test whether strValue is empty or is None
if (strValue) {
//do something
}
// Test wheter strValue is empty, but not None
if (strValue === "") {
//do something
}
xxxxxxxxxx
var myVar=null;
if(myVar === null){
//I am null;
}
if (typeof myVar === 'undefined'){
//myVar is undefined
}
xxxxxxxxxx
let myStr = null;
if (myStr === null || myStr.trim() === "") {
console.log("This is an empty string!");
} else {
console.log("This is not an empty string!");
}
xxxxxxxxxx
let str = "";
if (Boolean(str))
console.log("This is not an empty string!");
else
console.log("This is an empty string!");
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
function empty(str)
{
if (typeof str == 'undefined' || !str || str.length === 0 || str === "" || !/[^\s]/.test(str) || /^\s*$/.test(str) || str.replace(/\s/g,"") === "")
return true;
else
return false;
}
Run code snippet
xxxxxxxxxx
function empty(e) {
switch (e) {
case "":
case 0:
case "0":
case null:
case false:
case undefined:
return true;
default:
return false;
}
}
empty(null) // true
empty(0) // true
empty(7) // false
empty("") // true
empty((function() {
return ""
})) // false