0
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.

tushar garg
  • 69
  • 1
  • 6
  • 1
    possible duplicate of [How do I check for null values in javascript?](http://stackoverflow.com/questions/6003884/how-do-i-check-for-null-values-in-javascript) – Subodh Joshi Jul 24 '15 at 07:42
  • Do you have something processing those `'{$requestScope.someValue}'` string literals in some way? Because in your code above, you have the actual string `'{$requestScope.someValue}'`. – T.J. Crowder Jul 24 '15 at 07:47
  • @SubodhJoshi: No, that matches the title of this question, but not the content. – T.J. Crowder Jul 24 '15 at 07:48
  • no i dont have anything processing it. – tushar garg Jul 24 '15 at 07:49
  • @tushargarg: Then that's your problem, it's exactly like `var someValue = 'foo';`. The value you're checking is the actual string `'{$requestScope.someValue}`. – T.J. Crowder Jul 24 '15 at 07:51

1 Answers1

4

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875