Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well I can't follow the part with the <code>length</code> being <code>0</code>, did you actually run the loop? It should work just fine.</p> <p>But concerning the <code>typeof</code> operator, that operator (together with instanceof operator) is probably the biggest design flaw of JavaScript. It is near of being <strong>completely broken</strong>.</p> <p>Although <code>instanceof</code> still has its limited uses, <code>typeof</code> really has only one practical use case, which <strong>not happens</strong> to be checking the type of an object. </p> <p><strong>The JavaScript Typetable</strong></p> <pre><code>Value Class Type ------------------------------------- "foo" String string new String("foo") String object 1.2 Number number new Number(1.2) Number object true Boolean boolean new Boolean(true) Boolean object new Date() Date object new Error() Error object [1,2,3] Array object new Array(1, 2, 3) Array object new Function("") Function function /abc/g RegExp object (function in Nitro/V8) new RegExp("meow") RegExp object (function in Nitro/V8) {} Object object new Object() Object object </code></pre> <p>In the above table <em>Type</em> refers to the value the <code>typeof</code> operator returns. As you can see this is anything but consistent.</p> <p>The <em>Class</em> refers to the value of the internal <code>[[Class]]</code> property of an object.</p> <blockquote> <p><strong>From the Specification:</strong> <em>Class</em> can be one of the following values: <code>"Arguments"</code>, <code>"Array"</code>, <code>"Boolean"</code>, <code>"Date"</code>, <code>"Error"</code>, <code>"Function"</code>, <code>"JSON"</code>, <code>"Math"</code>, <code>"Number"</code>, <code>"Object"</code>, <code>"RegExp"</code>, <code>"String"</code></p> </blockquote> <p>In order to retrieve the value of <em>Class</em> one can has to make use of the <code>toString</code> method of <code>Object</code>.</p> <p><strong>Checking the Class of an Object</strong></p> <pre><code>function is(type, obj) { return Object.prototype.toString.call(obj).slice(8, -1) === type; } is('String', 'test'); // true is('String', new String('test')); // true </code></pre> <p>In the above code <code>Object.prototype.toString</code> gets called with <code>this</code> being set to the object which its <em>Class</em> value should be retrieved.</p> <p><strong>Checking whether a variable has been defined</strong></p> <pre><code>typeof foo !== 'undefined' </code></pre> <p>The above will check whether <code>foo</code> was actually declared or not, since just referencing it would result in a <code>ReferenceError</code>. This is the only thing <code>typeof</code> is actually useful for.</p> <p><strong>To Sum it up</strong></p> <p>Don't use <code>typeof</code> unless you're checking for the existence of a variable.</p>
    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