2

I'm using an API to get values to append to the DOM, out of the array a few of the values return null leaving them empty in the table. My question is it possible to return a string that states the "information is missing" instead of returning empty?

success: function(currency) {
    // loop through currency
    for (var i = 0; i < currency.length; i++) {
        if (currency[i].currency == userCurrency) {
            var $tr = $("<tr />");
            $tr.append($("<td />").text(currency[i].volume));
            $tr.append($("<td />").text(currency[i].latest_trade));
            $tr.append($("<td />").text(currency[i].bid));
            $tr.append($("<td />").text(currency[i].high));
            $("#theTable tbody").append($tr);
        }
    }
}
});
});
});
1
  • 1
    try .text(currency[i].bid || 'information is missing')); Commented Jan 5, 2016 at 8:55

1 Answer 1

5

use || "information is missing" like so:-

$tr.append( $("<td />").text(currency[i].volume || "information is missing"));

This will then use "information is missing" for any false value, ie: 0, null, undefined, "", false.

Though if you only want to check for null, you could use an inline if:-

$tr.append( $("<td />").text(currency[i].volume != null ? currency[i].volume : "information is missing"));
Sign up to request clarification or add additional context in comments.

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.