Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The difference is subtle.</p> <p>In JavaScript an <code>undefined</code> variable is a variable that as never been declared, or never assigned a value. Let's say you declare <code>var a;</code> for instance, then <code>a</code> will be <code>undefined</code>, because it was never assigned any value.</p> <p>But if you then assign <code>a = null;</code> then <code>a</code> will now be <code>null</code>. In JavaScript <code>null</code> is an object (try <code>typeof null</code> in a JavaScript console if you don't believe me), which means that null is a value (in fact even <code>undefined</code> is a value).</p> <p>Example:</p> <pre><code>var a; typeof a; # =&gt; "undefined" a = null; typeof null; # =&gt; "object" </code></pre> <p>This can prove useful in function arguments. You may want to have a default value, but consider null to be acceptable. In which case you may do:</p> <pre><code>function doSomething(first, second, optional) { if (typeof optional === "undefined") { optional = "three"; } // do something } </code></pre> <p>If you omit the <code>optional</code> parameter <code>doSomething(1, 2) then</code>optional will be the <code>"three"</code> string but if you pass <code>doSomething(1, 2, null)</code> then optional will be <code>null</code>.</p> <p>As for the equal <code>==</code> and strictly equal <code>===</code> comparators, the first one is weakly type, while strictly equal also checks for the type of values. That means that <code>0 == "0"</code> will return true; while <code>0 === "0"</code> will return false, because a number is not a string.</p> <p>You may use those operators to check between <code>undefined</code> an <code>null</code>. For example:</p> <pre><code>null === null # =&gt; true undefined === undefined # =&gt; true undefined === null # =&gt; false undefined == null # =&gt; true </code></pre> <p>The last case is interesting, because it allows you to check if a variable is either undefined or null and nothing else:</p> <pre><code>function test(val) { return val == null; } test(null); # =&gt; true test(undefined); # =&gt; true </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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.
    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