Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>All arrays in JavaScript contain <code>array.length</code> elements, starting with <code>array[0]</code> up until <code>array[array.length - 1]</code>. By definition, an array element with index <code>i</code> is said to be part of the array if <code>i</code> is between <code>0</code> and <code>array.length - 1</code> inclusive.</p> <p>That is, JavaScript arrays are linear, starting with zero and going to a maximum, and arrays don't have a mechanism for excluding certain values or ranges from the array. To find out if a value exists at a given position index (where index is 0 or a positive integer), you literally just use</p> <pre><code>if (index &lt; array.length) { // do stuff } </code></pre> <p>However, it <em>is</em> possible for some array values to be null, <code>undefined</code>, <code>NaN</code>, <code>Infinity</code>, 0, or a whole host of different values. For example, if you add array values by increasing the <code>array.length</code> property, any new values will be <code>undefined</code>. </p> <p>To determine if a given value is something meaningful, or has been defined. That is, <strong>not</strong> <code>undefined</code>, or <code>null</code>:</p> <pre><code>if (typeof array[index] !== 'undefined') { </code></pre> <p>or</p> <pre><code>if (typeof array[index] !== 'undefined' &amp;&amp; array[index] !== null) { </code></pre> <p>Interestingly, because of JavaScript's comparison rules, my last example can be optimised down to:</p> <pre><code>if (array[index] != null) { // The == and != operator consider null equal to only null or undefined </code></pre>
    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