{"id":283,"date":"2022-03-02T16:54:59","date_gmt":"2022-03-02T14:54:59","guid":{"rendered":"https:\/\/www.kopf.com.br\/kaplof\/?p=283"},"modified":"2022-03-02T16:55:03","modified_gmt":"2022-03-02T14:55:03","slug":"how-to-create-an-array-with-n-empty-elements-in-javascript","status":"publish","type":"post","link":"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/","title":{"rendered":"How to create an array with N empty elements in javascript"},"content":{"rendered":"\n<p>While revising an internal code, I stumbled upon the following construct:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Array.from({length: n});<\/code><\/pre>\n\n\n\n<p>This was new for me, and I though: what the hell is this? This cannot be regular javascript. I started debugging and realized that what developer wanted to do was to create an array with <strong>n<\/strong> empty items. And it seems that this is one of the many valid ways of doing that.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why not simply use <em>new Array(n)<\/em>?<\/h2>\n\n\n\n<p>The first thing that came into my mind is why didn&#8217;t he simply do:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>new Array(n);<\/code><\/pre>\n\n\n\n<p>This isn&#8217;t clearly document in the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/Array\" data-type=\"URL\" data-id=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/Array\">Array constructor<\/a>, but when you use the Array constructor passing the <em>arrayLength <\/em>parameter, the length is set to <em> <em>arrayLength <\/em> <\/em>but the array is not initialized with empty values.<\/p>\n\n\n\n<p>Looking at Chrome&#8217;s console is easy to understand the difference:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>> new Array(3)\n(3)\u00a0&#91;empty \u00d7 3]\n\n> Array.from({ length: 3 })\n(3)\u00a0&#91;undefined, undefined, undefined]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Other ways to achieve the same behaviour<\/h2>\n\n\n\n<p>If you still want to use the Array constructor, there are other ways to create an array with undefined values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>> &#91;...new Array(3)]\n(3)\u00a0&#91;undefined, undefined, undefined]\n\n> new Array(3).fill(undefined)\n(3)\u00a0&#91;undefined, undefined, undefined]<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Initializing the array with values from 0 to N-1<\/h2>\n\n\n\n<p>If you want to create an empty arrays with values from 0 to N-1 (or from 1 to N) there are different ways to achieve based on this <a href=\"https:\/\/stackoverflow.com\/questions\/3746725\/how-to-create-an-array-containing-1-n\">thread<\/a>:<\/p>\n\n\n\n<p>Using ES6:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Array.from(Array(10).keys())\n\/\/=> &#91;0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n\n\/\/ or\n\n&#91;...Array(10).keys()]\r\n\/\/=> &#91;0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n\n\/\/ from 1 to N\n\nArray.from({length: 10}, (_, i) => i + 1)\r\n\/\/=> &#91;1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<\/code><\/pre>\n\n\n\n<p>Without ES6:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>> new Int8Array(10).map((curr, index) => curr = index + 1)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Differently from what one my think, the array constructor doesn&#8217;t create an array with empty values. This articles shows you various ways of achieving it.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to create an array with N empty elements in javascript - Kaplof<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to create an array with N empty elements in javascript - Kaplof\" \/>\n<meta property=\"og:description\" content=\"Differently from what one my think, the array constructor doesn&#039;t create an array with empty values. This articles shows you various ways of achieving it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Kaplof\" \/>\n<meta property=\"article:published_time\" content=\"2022-03-02T14:54:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-02T14:55:03+00:00\" \/>\n<meta name=\"author\" content=\"Bruno Marotta\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bruno Marotta\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/\"},\"author\":{\"name\":\"Bruno Marotta\",\"@id\":\"https:\/\/www.kopf.com.br\/kaplof\/#\/schema\/person\/a096a6de30951e12938c6a800eeaa891\"},\"headline\":\"How to create an array with N empty elements in javascript\",\"datePublished\":\"2022-03-02T14:54:59+00:00\",\"dateModified\":\"2022-03-02T14:55:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/\"},\"wordCount\":209,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.kopf.com.br\/kaplof\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/\",\"url\":\"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/\",\"name\":\"How to create an array with N empty elements in javascript - Kaplof\",\"isPartOf\":{\"@id\":\"https:\/\/www.kopf.com.br\/kaplof\/#website\"},\"datePublished\":\"2022-03-02T14:54:59+00:00\",\"dateModified\":\"2022-03-02T14:55:03+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.kopf.com.br\/kaplof\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to create an array with N empty elements in javascript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.kopf.com.br\/kaplof\/#website\",\"url\":\"https:\/\/www.kopf.com.br\/kaplof\/\",\"name\":\"Kaplof\",\"description\":\"Sharing problems and solutions for software development\",\"publisher\":{\"@id\":\"https:\/\/www.kopf.com.br\/kaplof\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.kopf.com.br\/kaplof\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.kopf.com.br\/kaplof\/#organization\",\"name\":\"Kopf\",\"url\":\"https:\/\/www.kopf.com.br\/kaplof\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.kopf.com.br\/kaplof\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/i0.wp.com\/www.kopf.com.br\/kaplof\/wp-content\/uploads\/2015\/04\/Logo-grande.png?fit=78%2C76&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/www.kopf.com.br\/kaplof\/wp-content\/uploads\/2015\/04\/Logo-grande.png?fit=78%2C76&ssl=1\",\"width\":78,\"height\":76,\"caption\":\"Kopf\"},\"image\":{\"@id\":\"https:\/\/www.kopf.com.br\/kaplof\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.kopf.com.br\/kaplof\/#\/schema\/person\/a096a6de30951e12938c6a800eeaa891\",\"name\":\"Bruno Marotta\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.kopf.com.br\/kaplof\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8e15f82b4b9672c32c601286a40eb46f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8e15f82b4b9672c32c601286a40eb46f?s=96&d=mm&r=g\",\"caption\":\"Bruno Marotta\"},\"url\":\"https:\/\/www.kopf.com.br\/kaplof\/author\/bmarotta\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to create an array with N empty elements in javascript - Kaplof","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"How to create an array with N empty elements in javascript - Kaplof","og_description":"Differently from what one my think, the array constructor doesn't create an array with empty values. This articles shows you various ways of achieving it.","og_url":"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/","og_site_name":"Kaplof","article_published_time":"2022-03-02T14:54:59+00:00","article_modified_time":"2022-03-02T14:55:03+00:00","author":"Bruno Marotta","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Bruno Marotta","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/#article","isPartOf":{"@id":"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/"},"author":{"name":"Bruno Marotta","@id":"https:\/\/www.kopf.com.br\/kaplof\/#\/schema\/person\/a096a6de30951e12938c6a800eeaa891"},"headline":"How to create an array with N empty elements in javascript","datePublished":"2022-03-02T14:54:59+00:00","dateModified":"2022-03-02T14:55:03+00:00","mainEntityOfPage":{"@id":"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/"},"wordCount":209,"commentCount":0,"publisher":{"@id":"https:\/\/www.kopf.com.br\/kaplof\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/","url":"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/","name":"How to create an array with N empty elements in javascript - Kaplof","isPartOf":{"@id":"https:\/\/www.kopf.com.br\/kaplof\/#website"},"datePublished":"2022-03-02T14:54:59+00:00","dateModified":"2022-03-02T14:55:03+00:00","breadcrumb":{"@id":"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.kopf.com.br\/kaplof\/how-to-create-an-array-with-n-empty-elements-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.kopf.com.br\/kaplof\/"},{"@type":"ListItem","position":2,"name":"How to create an array with N empty elements in javascript"}]},{"@type":"WebSite","@id":"https:\/\/www.kopf.com.br\/kaplof\/#website","url":"https:\/\/www.kopf.com.br\/kaplof\/","name":"Kaplof","description":"Sharing problems and solutions for software development","publisher":{"@id":"https:\/\/www.kopf.com.br\/kaplof\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.kopf.com.br\/kaplof\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.kopf.com.br\/kaplof\/#organization","name":"Kopf","url":"https:\/\/www.kopf.com.br\/kaplof\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kopf.com.br\/kaplof\/#\/schema\/logo\/image\/","url":"https:\/\/i0.wp.com\/www.kopf.com.br\/kaplof\/wp-content\/uploads\/2015\/04\/Logo-grande.png?fit=78%2C76&ssl=1","contentUrl":"https:\/\/i0.wp.com\/www.kopf.com.br\/kaplof\/wp-content\/uploads\/2015\/04\/Logo-grande.png?fit=78%2C76&ssl=1","width":78,"height":76,"caption":"Kopf"},"image":{"@id":"https:\/\/www.kopf.com.br\/kaplof\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.kopf.com.br\/kaplof\/#\/schema\/person\/a096a6de30951e12938c6a800eeaa891","name":"Bruno Marotta","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.kopf.com.br\/kaplof\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8e15f82b4b9672c32c601286a40eb46f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8e15f82b4b9672c32c601286a40eb46f?s=96&d=mm&r=g","caption":"Bruno Marotta"},"url":"https:\/\/www.kopf.com.br\/kaplof\/author\/bmarotta\/"}]}},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.kopf.com.br\/kaplof\/wp-json\/wp\/v2\/posts\/283"}],"collection":[{"href":"https:\/\/www.kopf.com.br\/kaplof\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kopf.com.br\/kaplof\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kopf.com.br\/kaplof\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kopf.com.br\/kaplof\/wp-json\/wp\/v2\/comments?post=283"}],"version-history":[{"count":1,"href":"https:\/\/www.kopf.com.br\/kaplof\/wp-json\/wp\/v2\/posts\/283\/revisions"}],"predecessor-version":[{"id":284,"href":"https:\/\/www.kopf.com.br\/kaplof\/wp-json\/wp\/v2\/posts\/283\/revisions\/284"}],"wp:attachment":[{"href":"https:\/\/www.kopf.com.br\/kaplof\/wp-json\/wp\/v2\/media?parent=283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kopf.com.br\/kaplof\/wp-json\/wp\/v2\/categories?post=283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kopf.com.br\/kaplof\/wp-json\/wp\/v2\/tags?post=283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}