Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript null check
    primarykey
    data
    text
    <p>I've come across the following code:</p> <pre><code>function (data) { if (data != null &amp;&amp; data !== undefined) { // some code here } } </code></pre> <p>I'm somewhat new to JavaScript, but, from other questions I've been reading here, I'm under the impression that this code does not make much sense.</p> <hr> <del> In particular, <a href="https://stackoverflow.com/a/2559513/2088822">this answer</a> states that </del> <blockquote> <p><del>You'll get an error if you access an undefined variable in any context other than <code>typeof</code>. </del></p> </blockquote> <p><strong>Update:</strong> The (quote of the) answer above may be misleading. It should say <em>«an undeclared variable»</em>, instead of <em>«an undefined variable»</em>.</p> <p>As I found out, in the answers by <a href="https://stackoverflow.com/a/16672768/2088822"><em>Ryan ♦</em></a>, <a href="https://stackoverflow.com/a/16673024/2088822"><em>maerics</em></a>, and <a href="https://stackoverflow.com/a/16674810/2088822"><em>nwellnhof</em></a>, even when no arguments are provided to a function, its variables for the arguments are always declared. This fact also proves wrong the first item in the list below.</p> <hr> <p>From my understanding, the following scenarios may be experienced:</p> <ul> <li><p><del>The function was called with no arguments, thus making <code>data</code> an undefined variable, and raising an error on <code>data != null</code>.</del></p></li> <li><p>The function was called specifically with <code>null</code> (or <code>undefined</code>), as its argument, in which case <code>data != null</code> already protects the inner code, rendering <code>&amp;&amp; data !== undefined</code> useless.</p></li> <li><p>The function was called with a non-null argument, in which case it will trivially pass both <code>data != null</code> <strong>and</strong> <code>data !== undefined</code>.</p></li> </ul> <p><strong>Q: Is my understanding correct?</strong></p> <hr> <p>I've tried the following, in Firefox's console:</p> <pre><code>-- [15:31:31.057] false != null [15:31:31.061] true -- [15:31:37.985] false !== undefined [15:31:37.989] true -- [15:32:59.934] null != null [15:32:59.937] false -- [15:33:05.221] undefined != null [15:33:05.225] false -- [15:35:12.231] "" != null [15:35:12.235] true -- [15:35:19.214] "" !== undefined [15:35:19.218] true </code></pre> <p>I can't figure out a case where the <code>data !== undefined</code> <strong>after</strong> <code>data != null</code> might be of any use.</p>
    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.
 

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