tushar garg
tushar garg

Reputation: 69

how to check null in javascript?

var someValue1 = '${requestScope.someValue}';
    var someValue = '${requestScope.someValue}';
    var someValue = '${requestScope.someValue}';
    if(someValue1 !== null && someValue1 !== undefined && someValue1 !== '' && someValue1!=='null'){
        alert('Your New someValue1 = '+someValue1);
    };
    if(someValue !== null && someValue !== undefined && someValue !== '' && someValue!=='null' ){
        alert('Error : ' + someValue +"\nDescription : " + someValue);
    };

i want to execute only one alert box depending on the values but null checking is not working.

My both alert boxes are getting executed and that is my main problem. i cant understand why.

Upvotes: 0

Views: 164

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074969

All of your values are strings, because you've put them in quotes:

var someValue1 = '${requestScope.someValue}';
// --------------^-------------------------^

Consequently, they'll never be === null or === undefined. Moreover, unless you have something processing those that you haven't shown, they'll never be === '', either, because they're the literal string {$requestScope.someValue} (exactly like 'foo' is the literal string foo).

If you want to check the value, check the value itself, requestScope.someValue.

Upvotes: 4

Related Questions