Dhruv Tyagi
Dhruv Tyagi

Reputation: 812

Comparison of two String is not working in script

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);

Upvotes: 0

Views: 90

Answers (4)

Akhil Menon
Akhil Menon

Reputation: 306

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 != "")

Upvotes: 2

Tirthraj Barot
Tirthraj Barot

Reputation: 2679

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.

Upvotes: 1

Dhara Parmar
Dhara Parmar

Reputation: 8101

To check empty or null string in jquery:

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

Upvotes: 1

Akshay
Akshay

Reputation: 2229

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

Simply use :-

if(entry.personalization == null)

Upvotes: 0

Related Questions