Using a ternary operator to set a variable

javascript, xml

Solution

The shortest way:

var rating = (data.getElementsByTagName('overall_average')[0].childNodes[0] || {}).nodeValue || 'It is empty';

Problem

I am trying to use a ternary operator to check if the value of an XML element is null. If it is then I want the variable to be one thing. If not then I would like it to return the value of element. This is what I have so far. ``` var rating = data.getElementsByTagName("overall_average")[0].childeNodes[0].length > 0 ? data.getElementsByTagName("overall_average")[0].childeNodes[0].nodeValue : "It is empty"; ```

Original source