0

Actually in my function I have to compare two strings, the one string I fetch through the db and compare to the null string but it's not working.. Here is my Code:

   order.items.forEach(function(entry) {
                            result += '<tr>' 
                                      + '<td>'+'<font size=2>'+ a++ + '</font>'+ '</td>'
                                      + '<td>' +'<font size=2>'+ entry.title + '</font>'+ '</td>'
                                      + '<td>' +'<font size=2>'+ entry.quantity + '</font>'+'</td>'        
                                    if(entry.personalization == 'null')     //here is the problem                                                       
                                      + '<td>' +'<font size=2>'+ 'No Personalization' + '</font>'+'</td>'
                                    else
                                      + '<td>' +'<font size=2>'+ entry.personalization + '</font>'+'</td>'                                          


                                    + '</tr>';
                        })

                     result +='</table>';

$('.modal-body').html(result);
10
  • 1
    null not 'null' Commented Jun 17, 2016 at 10:51
  • compare it like if(!(entry.personalization)) Commented Jun 17, 2016 at 10:52
  • @Ripun What if entry.personalization is set to zero? Will your tip still hold? Commented Jun 17, 2016 at 10:58
  • @spender he is assuming the entry.personalization as null value when checking so if it is set to 0 then there is no meaning on checking for null isn't it ? Commented Jun 17, 2016 at 10:59
  • Can you tell us what the value of typeof entry.personalization is? Commented Jun 17, 2016 at 10:59

4 Answers 4

2

Use console.log(entry.personalization) to check value.

Dhara's answer should work...

I also use it for null check.

(!entry.personalization)  

or try

(entry.personalization != "")
Sign up to request clarification or add additional context in comments.

Comments

1

To check empty or null string in jquery:

if (!entry.personalization) {
    // is empty
}

1 Comment

@KartikeyaKhosla $.trim() is used for Remove the white spaces at the start and at the end of the string.
1

As mentioned in your question, if one string i fetched by you and the other string is "null", It is a clear question of string comparison and not checking null...

Javascript has localeCompare() method to compare strings..

You should use

entry.personalization.localeCompare("null");

or its inverse

var n = "null";
n.localeCompare(entry.personalization);

this function returns boolean.

7 Comments

what does it return console.log(entry.personalization)
actually i dont know how to use console.log() so i will update the result soon
no problem... let me tell you.... console.log() is a function which will log the values inside the console... If you want to see it, press F-12 on your keyboard with a browser open and go to its console tab... you will see the value of console.log() in it...
that means your entry.personalization holds null.. and not "null"
|
0

You don't need to wrap null around ''.

Simply use :-

if(entry.personalization == null)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.