Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is the <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Operators/Special_Operators" rel="nofollow noreferrer">conditional</a> operator, it is equivalent to something like this:</p> <pre><code>if (pattern.Gotoccurance.score != null) { pattern.Gotoccurance.score; } else { '0'; } </code></pre> <p>But I think that an assignment statement is missing in the code you posted, like this:</p> <pre><code>var score = pattern.Gotoccurance.score !=null ? pattern.Gotoccurance.score : '0'; </code></pre> <p>The <code>score</code> variable will be assigned if <code>pattern.Gotoccurance.score</code> is not null:</p> <pre><code>var score; if (pattern.Gotoccurance.score != null) { score = pattern.Gotoccurance.score; } else { score = '0'; } </code></pre> <p>A common pattern to do this kind of 'default value' assignments in JavaScript is to use the logical OR operator (<code>||</code>) :</p> <pre><code>var score = pattern.Gotoccurance.score || '0'; </code></pre> <p>The value of <code>pattern.Gotoccurance.score</code> will be assigned to the <code>score</code> variable only if that value is not <em>falsy</em> (falsy values are <code>false</code>, <code>null</code>, <code>undefined</code>, <code>0</code>, zero-length string or <code>NaN</code>).</p> <p>Otherwise, if it's falsy <code>'0'</code> will be assigned.</p> <p><strong>Update:</strong> The performance will be equivalent, you should focus on readability, I try to use the ternary operator on expressions that are very simple, and you can also improve the formatting, splitting it up in two lines to make it more readable:</p> <pre><code>var status = (age &gt;= 18) ? "adult" : "minor"; </code></pre> <p>Related question:</p> <ul> <li><a href="https://stackoverflow.com/questions/160218/to-ternary-or-not-to-ternary">To Ternary or Not To Ternary?</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload