Dhruv Tyagi Dhruv Tyagi - 5 months ago 32
jQuery Question

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

Answer

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.

Comments