close

[Solved] How to check if a value is not null and not empty string in JS

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error How to check if a value is not null and not empty string in JS in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How to check if a value is not null and not empty string in JS Error Occurs?

Today I get the following error How to check if a value is not null and not empty string in JS in javascript.

How To Solve check if a value is not null and not empty string in JS Error ?

  1. How To Solve check if a value is not null and not empty string in JS Error ?

    To Solve check if a value is not null and not empty string in JS Error Try:
    if (str && str.trim().length) { //... } String.prototype.isEmpty = function() {
    return (this.length === 0 || !this.trim());
    };

  2. How to check if a value is not null and not empty string in JS

    To Solve check if a value is not null and not empty string in JS Error Try:
    if (str && str.trim().length) { //... } String.prototype.isEmpty = function() {
    return (this.length === 0 || !this.trim());
    };

Solution 1

For checking if a string is empty, null or undefined I use:

function isEmpty(str) {
    return (!str || str.length === 0 );
}

For checking if a string is blank, null or undefined I use:

function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}

For checking if a string is blank or contains only white-space:

String.prototype.isEmpty = function() {
    return (this.length === 0 || !this.trim());
};

Solution 2

Try:

if (str && str.trim().length) {  
    //...
}

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment