{"id":7380,"date":"2021-08-04T11:28:13","date_gmt":"2021-08-04T11:28:13","guid":{"rendered":"https:\/\/www.scmgalaxy.com\/tutorials\/?p=7380"},"modified":"2022-04-13T16:30:47","modified_gmt":"2022-04-13T16:30:47","slug":"how-do-you-use-arrays-in-javascript","status":"publish","type":"post","link":"https:\/\/www.scmgalaxy.com\/tutorials\/how-do-you-use-arrays-in-javascript\/","title":{"rendered":"How do you use Arrays in JavaScript?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Array<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Arrays are a collection of data items stored under a single name. Array provides a mechanism for declaring and accessing several data items with only one identifier, thereby simplifying the task of data management.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We use an array when we have to deal with multiple data items. Arrays are a special type of object. The typeof operator in JavaScript returns &#8220;object&#8221; for arrays.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Declaration and initialization of Array<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Using Array Literal<\/strong><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Syntax: &#8211; var array_name = [ ];<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/fa503b7d5abe497880300ce5f1a37b78.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Declaration and initialization of Array<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Using Array Literal<\/strong><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax: &#8211; var array_name = [value1, value2, value_n];<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/748c8cf51854dd27059a44340873cf5c.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Declaration and initialization of Array<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Using Array Constructor<\/strong><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax: &#8211; var array_name = new Array( );<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/39108e2f54d937813bd1efd74df06da2.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Declaration and initialization of Array<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Using Array Constructor<\/strong><\/li><\/ul>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/d30543b26f06a19e03b35f113b6010d9.js\"><\/script>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/fbec2a8ebcebc6245f963a478a99e90d.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\">This will create an empty array with 5 lengths. So this is not a good idea to use Array<br>Constructor if you have only a single numeric value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note &#8211; By default, the array starts with index 0.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Important Points<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>JavaScript arrays are zero-indexed: the first element of an array is at index 0.<\/li><li>Using an invalid index number returns undefined.<\/li><li>It&#8217;s possible to quote the JavaScript array indexes as well (e.g., geek[&#8216;2&#8217;] instead of geek[2]), although it&#8217;s not necessary.<\/li><li>Arrays cannot use strings as element indexes but must use integers.<\/li><li>There is no associative array in JavaScript. geek[\u201cfees\u201d] = 200;<\/li><li>No advantage to use Array Constructor so better to use Array Literal for creating Arrays in JavaScript.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing Array Elements<\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/6165a1f848c416fd2b00af0a95c3e9c9.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing Array Elements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Access all at once<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/95e2b35d5cb8e3ad1bd06200984f18f3.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Modifying Array Elements<\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/a54ea2809380ab1f44aa5286960663d6.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Removing Array Elements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Array elements can be removed using the delete operator. This operator sets the array element it is invoked on to undefined but does not change the array\u201fs length.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syantx :- delete Array_name[index];<br>Ex:- delete dev[0];<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Length Property<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The length property retrieves the index of the next available position at the end of the array. The length property is automatically updated as new elements are added to the array. For this reason, length is commonly used to iterate through all elements of an array.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/7f3af0f0cbf9f7287e95ecceb376ec6b.js\"><\/script>\n","protected":false},"excerpt":{"rendered":"<p>Array Arrays are a collection of data items stored under a single name. Array provides a mechanism for declaring and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[16],"tags":[490,5613,5318,5144,5095,1073],"class_list":["post-7380","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-array","tag-array-operators","tag-elements","tag-functions","tag-javascript","tag-properties"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How do you use Arrays in JavaScript? - scmGalaxy<\/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.scmgalaxy.com\/tutorials\/how-do-you-use-arrays-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How do you use Arrays in JavaScript? - scmGalaxy\" \/>\n<meta property=\"og:description\" content=\"Array Arrays are a collection of data items stored under a single name. Array provides a mechanism for declaring and [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/how-do-you-use-arrays-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"scmGalaxy\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-04T11:28:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-13T16:30:47+00:00\" \/>\n<meta name=\"author\" content=\"rajeshkumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rajeshkumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-do-you-use-arrays-in-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-do-you-use-arrays-in-javascript\\\/\"},\"author\":{\"name\":\"rajeshkumar\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\"},\"headline\":\"How do you use Arrays in JavaScript?\",\"datePublished\":\"2021-08-04T11:28:13+00:00\",\"dateModified\":\"2022-04-13T16:30:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-do-you-use-arrays-in-javascript\\\/\"},\"wordCount\":343,\"commentCount\":0,\"keywords\":[\"array\",\"Array operators\",\"elements\",\"functions\",\"Javascript\",\"properties\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-do-you-use-arrays-in-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-do-you-use-arrays-in-javascript\\\/\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-do-you-use-arrays-in-javascript\\\/\",\"name\":\"How do you use Arrays in JavaScript? - scmGalaxy\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\"},\"datePublished\":\"2021-08-04T11:28:13+00:00\",\"dateModified\":\"2022-04-13T16:30:47+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-do-you-use-arrays-in-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-do-you-use-arrays-in-javascript\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-do-you-use-arrays-in-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How do you use Arrays in JavaScript?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\",\"name\":\"scmGalaxy\",\"description\":\"A Platform for Software Engineers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\",\"name\":\"rajeshkumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"caption\":\"rajeshkumar\"},\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/author\\\/rajeshkumar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How do you use Arrays in JavaScript? - scmGalaxy","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.scmgalaxy.com\/tutorials\/how-do-you-use-arrays-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"How do you use Arrays in JavaScript? - scmGalaxy","og_description":"Array Arrays are a collection of data items stored under a single name. Array provides a mechanism for declaring and [&hellip;]","og_url":"https:\/\/www.scmgalaxy.com\/tutorials\/how-do-you-use-arrays-in-javascript\/","og_site_name":"scmGalaxy","article_published_time":"2021-08-04T11:28:13+00:00","article_modified_time":"2022-04-13T16:30:47+00:00","author":"rajeshkumar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rajeshkumar","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-do-you-use-arrays-in-javascript\/#article","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-do-you-use-arrays-in-javascript\/"},"author":{"name":"rajeshkumar","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361"},"headline":"How do you use Arrays in JavaScript?","datePublished":"2021-08-04T11:28:13+00:00","dateModified":"2022-04-13T16:30:47+00:00","mainEntityOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-do-you-use-arrays-in-javascript\/"},"wordCount":343,"commentCount":0,"keywords":["array","Array operators","elements","functions","Javascript","properties"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.scmgalaxy.com\/tutorials\/how-do-you-use-arrays-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-do-you-use-arrays-in-javascript\/","url":"https:\/\/www.scmgalaxy.com\/tutorials\/how-do-you-use-arrays-in-javascript\/","name":"How do you use Arrays in JavaScript? - scmGalaxy","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website"},"datePublished":"2021-08-04T11:28:13+00:00","dateModified":"2022-04-13T16:30:47+00:00","author":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361"},"breadcrumb":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-do-you-use-arrays-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.scmgalaxy.com\/tutorials\/how-do-you-use-arrays-in-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-do-you-use-arrays-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.scmgalaxy.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"How do you use Arrays in JavaScript?"}]},{"@type":"WebSite","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website","url":"https:\/\/www.scmgalaxy.com\/tutorials\/","name":"scmGalaxy","description":"A Platform for Software Engineers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.scmgalaxy.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361","name":"rajeshkumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","caption":"rajeshkumar"},"url":"https:\/\/www.scmgalaxy.com\/tutorials\/author\/rajeshkumar\/"}]}},"_links":{"self":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7380","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/comments?post=7380"}],"version-history":[{"count":1,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7380\/revisions"}],"predecessor-version":[{"id":7382,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7380\/revisions\/7382"}],"wp:attachment":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/media?parent=7380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/categories?post=7380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/tags?post=7380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}