`if (0 in array)` — is this even legal?


For now I'm writing a complex of laboratory works about coding in JavaScript. I know that there is a construction like

if ('field' in object) { /* Do something with object.field */ }

to be used to determine that a variable called field really exists in object even if it equals to undefined.

And my Firefox Developer Edition 44 is able to determine if there is a field in an array like this:

if (0 in array) { /* Do something with first element */ }

<strong>The question</strong>: is this method legal? Is it a part of a living standard or not?

<hr />

<strong>What it is <em>supposed</em> to do</strong>:<br /> Let q be an array with such elements:

[ 5, <1 empty slot>, undefined, 5 ]


<ul><li>0 in q, 2 in q, 3 in q equals to true;</li> <li>1 in q equals to false.</li> </ul>



if (0 in array) — is this even legal?


Sure. The in operator checks to see if a property exists in the object (directly, or via the prototype chain). The left-hand operand is the name of the property, the right-hand operand is the object to check. If the left-hand operand isn't a string or Symbol, it's coerced to a string. This is all completely within <a href="http://www.ecma-international.org/ecma-262/6.0/index.html" rel="nofollow">the specification</a>:


<a href="http://www.ecma-international.org/ecma-262/6.0/index.html#sec-relational-operators-runtime-semantics-evaluation" rel="nofollow">The in operator</a> (scroll down to <em>RelationalExpression : RelationalExpression in ShiftExpression</em>)

</li> <li>

<a href="http://www.ecma-international.org/ecma-262/6.0/index.html#sec-topropertykey" rel="nofollow">The abstract ToPropertyKey operation</a>

</li> <li>

<a href="http://www.ecma-international.org/ecma-262/6.0/index.html#sec-hasproperty" rel="nofollow">The abstract HasProperty operation</a>

</li> </ul>

JavaScript's untyped arrays are objects (and <a href="http://blog.niftysnippets.org/2011/01/myth-of-arrays.html" rel="nofollow">not really arrays</a>, although engines optimize that when they can). Array indexes are property names (and technically strings, though engines optimize that when they can). So you can use in to check if an array contains a property by using an "array index" with in and the array. (You can also use hasOwnProperty: theArray.hasOwnProperty(0), but <a href="http://jsperf.com/in-vs-hasownproperty-for-arrays" rel="nofollow">it's slower with arrays on modern engines</a>.)


What it is supposed to do:

Let q be an array with such elements:

[ 5, <1 empty slot>, undefined, 5 ]


<ul><li>0 in q, 2 in q, 3 in q equals to true;</li> <li>1 in q equals to false.</li> </ul></blockquote>

Yes, that's perfectly valid:

<pre class="snippet-code-js lang-js prettyprint-override">var q = []; q[0] = 5; q[2] = undefined; q[3] = 5; for (var i = 0; i < q.length; ++i) { snippet.log(i + " in q? " + (i in q)); } <pre class="snippet-code-html lang-html prettyprint-override"><!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>


  • Download a file from an FTP using Curl with PHP
  • How to use 'defuse/php-encryption'?
  • Python->Beautifulsoup->Webscraping->Looping over URL (1 to 53) and saving Results
  • Einstein Riddle using Prolog
  • How to load files to local file system with vibed?
  • Legal legacy code using pointers suddenly becomes UB
  • Qt: closing modal dialog closes the program
  • c#.NET USB device persistent identifier
  • DirectX game with no prerequisite software to run
  • C Language: Why does malloc() return a pointer, and not the value?
  • What version of Java should I use with Cassandra 2.0?
  • How to stamp out template in self contained custom elements with vanilla js?
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • extjs4 catching the scroll event on panel
  • there is no graph with tensorboard
  • Jackson Parser: ignore deserializing for type mismatch
  • Word Open XML Mail Merge
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Why is an OPTIONS request sent to the server?
  • What is Eclipse's Declaration View used for?
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • When should I choose bucket sort over other sorting algorithms?
  • Jquery - Jquery Wysiwyg return html as a string
  • SVN: Merging two branches together
  • Unanticipated behavior
  • sending mail using smtp is too slow
  • Append folder name and increment by 1 using batch script
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal