How to Check for a Null or Empty String in JavaScript
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!"
*/
// Test whether strValue is empty or is None
if (strValue) {
//do something
}
// Test wheter strValue is empty, but not None
if (strValue === "") {
//do something
}
let myStr = null;
if (myStr === null || myStr.trim() === "") {
console.log("This is an empty string!");
} else {
console.log("This is not an empty string!");
}
javascript check if undefined or null or empty string
// simple check do the job
if (myString) {
// comes here either myString is not null,
// or myString is not undefined,
// or myString is not '' (empty).
}
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, ...
}
var string = "not empty";
if(string == ""){
console.log("Please Add");
}
else{
console.log("You can pass"); // console will log this msg because our string is not empty
}