
{"id":5553,"date":"2026-10-16T16:55:59","date_gmt":"2026-10-16T20:55:59","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=5553"},"modified":"2026-10-16T16:55:59","modified_gmt":"2026-10-16T20:55:59","slug":"another-javascript-trap-with-arrays","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=5553","title":{"rendered":"Another Javascript trap with arrays"},"content":{"rendered":"<p>JavaScript arrays are a famous source of trouble. E.g. when you access index out of bounds, you don&#8217;t get an exception, you get <code>undefined<\/code>. This, by the way, breaks type checking at TypeScript, but I digress. It turns that in V8, the most popular JavaScript engine, maximum possible array index is 4294967295, or 2<sup>32<\/sup>-1.<\/p>\n<p>What happens if you use a bigger index? No problem, it would be converted to a <em>named property<\/em>, but the length of the array would remain unchanged. Basically <code>arr[5_000_000_000]=42<\/code> is equivalent to <code>arr.random_name = 42<\/code>.<\/p>\n<p>Consider this code:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction len(idx) {\r\n  const x = &#x5B;];\r\n  x&#x5B;idx] = 42;\r\n  return x.length;\r\n}\r\n\r\nconsole.log(len(1)); \/\/ 2\r\nconsole.log(len(100)); \/\/ 101\r\nconsole.log(len(4_000_000_000)); \/\/ 4000000001\r\nconsole.log(len(5_000_000_000)); \/\/ 0\r\n<\/pre>\n<p>Isn&#8217;t JavaScript fun?<\/p>\n<p>TypeScript version behaves the same, adding type annotations does not change much.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript arrays are a famous source of trouble. E.g. when you access index out of bounds, you don&#8217;t get an exception, you get undefined. This, by the way, breaks type <a href=\"https:\/\/ikriv.com\/blog\/?p=5553\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4,29],"tags":[],"class_list":["entry","author-ikriv","post-5553","post","type-post","status-publish","format-standard","category-hack","category-typescript"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5553"}],"version-history":[{"count":1,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5553\/revisions"}],"predecessor-version":[{"id":5554,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5553\/revisions\/5554"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}