Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a difference between an existing variable that happens to contain the <code>undefined</code> value, and a variable that does not exist at all. If a variable name doesn't exist, then it's an error to try to reference it.</p> <p>The <code>typeof</code> operator is a special case: it does accept a name even if there is no such variable of that name. But only when the name is actually used <em>with</em> the <code>typeof</code> operator.</p> <p>In your function example, you use a name that does not exist, and there's no <code>typeof</code> operator <em>at the point where the name is used</em>. That's where the error occurs. It doesn't matter that you are passing the name into a function that will use <code>typeof</code>; that function never gets called because the error has already happened.</p> <p>Your function would work as expected if you give it an existing variable name. Then it would tell you if that variable has the <code>undefined</code> value or not:</p> <pre><code>var boo; // now boo exists but has the undefined value console.log( isUndefined(boo) ); // will log 'true' </code></pre> <p>If you were checking a global variable, it would work if you said <code>window.boo</code> instead of just <code>boo</code>. That's because it is not an error to reference a nonexistent property of an object; it just gives you the <code>undefined</code> value when you do this:</p> <pre><code>// logs 'true' if there is no global 'boo' console.log( isUndefined(window.boo) ); </code></pre> <p>That wouldn't work if you wanted to check whether a <em>local</em> variable exists, though, because it won't be a property of the <code>window</code> object.</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.
    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