11

I'm looking for most efficient way to output empty string instead of "null" for null values. Is there a way to do this without conditional statements (i.e. if or ternary (? :) operator).

Code sample as follows:

$.each(data, function(index, item) {
  return $("div#SearchResults").append(item.PhoneNumber);
}

when item.PhoneNumber is null, the page renders with a string "null" inside the SearchResults div, instead I wanted to be empty.

4
  • 1
    Why? Why would you want to avoid evaluating your variables before output? Commented Jul 9, 2012 at 10:22
  • I thought there would be a more efficient way, i.e. some sort of built-in function. I'm surprised there isn't one in either JavaScript or jQuery :( Commented Jul 9, 2012 at 10:24
  • Well, the || (or) operator does this, but the variable's still evaluated first, just without an explicit if or ternary. Commented Jul 9, 2012 at 10:26
  • 1
    The conversion of null to string is hard coded at implementation level to be "null" and cannot be changed by user-made code Commented Jul 9, 2012 at 10:31

4 Answers 4

23
$.each(data, function(index, item) {
  return $("div#SearchResults").append(item.PhoneNumber || "");
}

With $.trim (unexpectedly converts null and undefined to empty string):

$.each(data, function(index, item) {
  return $("div#SearchResults").append($.trim(item.PhoneNumber));
}

Unexpected because it's both unintuitive and undocumented.

Sign up to request clarification or add additional context in comments.

1 Comment

Upvoting because we came up with the same answer at around the same time and, also, because I don't understand the down-vote.
8

If you're really determined not to use an if or ternary to evaluate the variable, then there is the possibility of using the || operator:

$.each(data, function(index, item) {
  return $("div#SearchResults").append(item.PhoneNumber || '');
}

But, honestly, just use a conditional operator, it's what they're meant for, and helps enforce a certain amount of security in your script's execution and output.

2 Comments

At most I'd go for a === null ternarny but not if statement :P
I agree with the your approach to the evaluation method; but I'd still, regardless, explicitly evaluate the variable/return value, rather than relying on the above method (albeit it works, it just feels... wrong, slightly).
1

Try this,

$.each(data, function(index, item) {
  return $("div#SearchResults").append(item.PhoneNumber?item.PhoneNumber:"");
}

2 Comments

From the OPs question Is there a way to do this without conditional statements (i.e. if or ? :)
I didn't down-vote, but the question specifically requests avoiding the ternary operator (as well as the if), though it's not referred to by name; so is easily missed.
-1

Without JQuery:

$.each(data, function(index, item) {
  return $("div#SearchResults").append([item.PhoneNumber].join(''));
}

1 Comment

'Without jQuery', but it's still jQuery?

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.