Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen to check for undefined and when to check for null
    primarykey
    data
    text
    <p><strong>[Bounty Edit]</strong></p> <p>I'm looking for a good explanation when you should set/use <code>null</code> or <code>undefined</code> and where you need to check for it. Basically what are common practices for these two and is really possible to treat them separately in generic maintainable codee?</p> <p>When can I safely check for <code>=== null</code>, safely check for <code>=== undefined</code> and when do I need to check for both with <code>== null</code></p> <p>When should you use the keyword <code>undefined</code> and when should one use the keyword <code>null</code> </p> <p>I have various checks in the format of </p> <p><code>if (someObj == null)</code> or <code>if (someObj != null)</code> which check for both null and undefined. I would like to change all these to either <code>=== undefined</code> or <code>=== null</code> but I'm not sure how to guarantee that it will only ever be one of the two but not both.</p> <p>Where should you use checks for <code>null</code> and where should you use checks for <code>undefined</code></p> <p>A concrete example:</p> <pre><code>var List = []; // ordered list contains data at odd indexes. var getObject = function(id) { for (var i = 0; i &lt; List.length; i++) { if (List[i] == null) continue; if (id === List[i].getId()) { return List[i]; } } return null; } var deleteObject = function(id) { var index = getIndex(id) // pretty obvouis function // List[index] = null; // should I set it to null? delete List[index]; // should I set it to undefined? } </code></pre> <p>This is just one example of where I can use both <code>null</code> or <code>undefined</code> and I don't know which is correct.</p> <p>Are there any cases where you must check for both <code>null</code> and <code>undefined</code> because you have no choice?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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