Is there a shorter/cleaner way to do the null/undefined testing?
Most of the above solutions didn't work for me quite out of the box, since I only wanted to apply this to a single element in a foreach so I modified the accepted answer's approach a bit:
foreach