{"id":206,"date":"2020-01-10T22:53:00","date_gmt":"2020-01-10T22:53:00","guid":{"rendered":""},"modified":"2024-09-13T15:04:52","modified_gmt":"2024-09-13T15:04:52","slug":"javascript-tutorial-series-arrays","status":"publish","type":"post","link":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/01\/10\/javascript-tutorial-series-arrays\/","title":{"rendered":"JavaScript Tutorial Series &#8211; Arrays"},"content":{"rendered":"<p>\nIn JavaScript, arrays store a list of items. An item may be a primitive such as a string or number. An item may also be an object or another array. Indexing starts at 0. <br \/>\nArrays are objects. To determine if a variable is an array, use <\/p>\n<pre><code>\n    Object.prototype.toString.call(theArray) === '[object Array]' \n<\/code><\/pre>\n<p>\nor the newer way of <\/p>\n<pre><code>\n    Array.isArray(theArray);\n<\/code><\/pre>\n<p>Some of the more commonly used actions that may be performed on an array include:<\/p>\n<p>Initialize:<\/p>\n<pre><code>\n    let myArray = ['a', 'b', 'c'];\n<\/code><\/pre>\n<p>\nArrays can be created by calling:<\/p>\n<pre><code>\n    let myArray2 = new Array('a', 'b', 'c');\n<\/code><\/pre>\n<p>\nThe first way is much simpler, so there&#8217;s not really any reason to use this way.<\/p>\n<p>\nTo retrieve the number of items in the list, do the following:<\/p>\n<pre><code>\n    let count = myArray.length;\n<\/code><\/pre>\n<p>Individual items in an array may be accessed by it&#8217;s index. To access the first item, call <\/p>\n<pre><code>\n    let firstItem = myArray[0];\n<\/code><\/pre>\n<p>\nTo access the last item, call<\/p>\n<pre><code>\n    let lastItem = myArray[myArray.length-1];\n<\/code><\/pre>\n<p>\nAdd an item to the end of the list:<\/p>\n<pre><code>\n    myArray.push('d');\n<\/code><\/pre>\n<p>\nAdd an item to the beginning of the list:<\/p>\n<pre><code>\n    myArray.unshift('d');\n<\/code><\/pre>\n<p>\nRemove last item:<\/p>\n<pre><code>\n    myArray.pop();\n<\/code><\/pre>\n<p>\nRemove first item:<\/p>\n<pre><code>\n    myArray.shift();\n<\/code><\/pre>\n<p>\nGet the number of items in an array:<\/p>\n<pre><code>\n    let count = myArray.length();\n<\/code><\/pre>\n<p>\nGet the index of an item in an array: Note: this returns -1 if the item is not found:<\/p>\n<pre><code>\n    let index = myArray.indexOf('b');\n<\/code><\/pre>\n<p>\nReturn a certain range of items, based on index, from an array:<\/p>\n<pre><code>\n    let subset = myArray.slice(1, 2);\n<\/code><\/pre>\n<p>I tend to use objects more than arrays, but one common way to use arrays is when extracting pieces of a string. A string, such as an id, may contain several pieces of information separated by a delimiter. For example, the following string could represent a date with a dash as the delimiter:<\/p>\n<p>01-20-2020<\/p>\n<p>The month, date and year could be extracted as follows:<\/p>\n<pre><code>\n    let dateStr = '01-20-2020';\n    let datePieces = dateStr.split('-');\n    console.log('Month: ' + datePieces[0]);\n    console.log('Day: ' + datePieces[1]);\n    console.log('Year: ' + datePieces[2]);\n<\/code><\/pre>\n<p>\nAn array may store another array, creating a multidimensional array. For example:<\/p>\n<pre><code>\n    let array2d = [[0,0], [3,5]];\n<\/code><\/pre>\n<p>Arrays can be searched using the Array.prototype.find function. A callback function which defines the search criteria is passed into the find function. The result is then returned.<br \/>\nExample:<\/p>\n<pre><code>\n    function search (item) {\n        return item === 'b';\n    }\n    let bElement = myArray.find(search);    \n<\/code><\/pre>\n<p><strong>Implementation:<\/strong><\/p>\n<p>Inside the javascript_tutorial.js file created in the <a href=\"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-tutorial-series-set-up\/\" target=\"_blank\" rel=\"noopener\">JavaScript set up tutorial<\/a>, add the following lines that are in bold:<\/p>\n<pre>\/\/ JavaScript Tutorial\n\n\/*\n* This file will add JavaScript\n* to a website.\n*\/\n\n(function() {\n    let doc = document;\n    <strong>\n    let defaultLocation = [-86.7816, 36.1627]; \/\/ longitude, latitude\n    <\/strong>\n})(); \n<\/pre>\n<p>The JavaScript tutorial series starts with <a href=\"https:\/\/zofxare.com\/zofxare\/blog\/2019\/12\/27\/javascript-tutorial-series-set-up\/\/\" rel=\"noopener noreferrer\" target=\"_blank\">this post<\/a>.<\/p>\n<p>(paid links)<br \/>\n<a href=\"https:\/\/amzn.to\/39aSKtV\" rel=\"noopener\" target=\"_blank\">More JavaScript<\/a><\/p>\n<p><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=144934013X&amp;asins=144934013X&amp;linkId=1c564951ff9e32885fd78e6d3e51cd88&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=1119366445&amp;asins=1119366445&amp;linkId=dd23c2d87c50139ea87f57684a12c407&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=0596517742&amp;asins=0596517742&amp;linkId=6d3fc10c6f7129dfd9a699a17a1caa01&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><iframe frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\" scrolling=\"no\" src=\"\/\/ws-na.amazon-adsystem.com\/widgets\/q?ServiceVersion=20070822&amp;OneJS=1&amp;Operation=GetAdHtml&amp;MarketPlace=US&amp;source=ss&amp;ref=as_ss_li_til&amp;ad_type=product_link&amp;tracking_id=zofxare-20&amp;language=en_US&amp;marketplace=amazon&amp;region=US&amp;placement=1497408180&amp;asins=1497408180&amp;linkId=13300f4284c721f7e5b0da78795da9eb&amp;show_border=true&amp;link_opens_in_new_window=true\" style=\"height: 240px; width: 120px;\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In JavaScript, arrays store a list of items. An item may be a primitive such as a string or number. An item may also be an object or another array. Indexing starts at 0. Arrays are objects. To determine if a variable is an array, use Object.prototype.toString.call(theArray) === &#8216;[object Array]&#8217; or the newer way of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","om_disable_all_campaigns":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[340,339,341],"tags":[],"class_list":["post-206","post","type-post","status-publish","format-standard","hentry","category-javascript","category-javascript-tutorials","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JavaScript Tutorial Series - Arrays - Zofxare Blog Home<\/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:\/\/zofxare.com\/zofxare\/blog\/2020\/01\/10\/javascript-tutorial-series-arrays\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Tutorial Series - Arrays - Zofxare Blog Home\" \/>\n<meta property=\"og:description\" content=\"In JavaScript, arrays store a list of items. An item may be a primitive such as a string or number. An item may also be an object or another array. Indexing starts at 0. Arrays are objects. To determine if a variable is an array, use Object.prototype.toString.call(theArray) === &#039;[object Array]&#039; or the newer way of [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/zofxare.com\/zofxare\/blog\/2020\/01\/10\/javascript-tutorial-series-arrays\/\" \/>\n<meta property=\"og:site_name\" content=\"Zofxare Blog Home\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-10T22:53:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-13T15:04:52+00:00\" \/>\n<meta name=\"author\" content=\"adminuser\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"adminuser\" \/>\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:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/01\\\/10\\\/javascript-tutorial-series-arrays\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/01\\\/10\\\/javascript-tutorial-series-arrays\\\/\"},\"author\":{\"name\":\"adminuser\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\"},\"headline\":\"JavaScript Tutorial Series &#8211; Arrays\",\"datePublished\":\"2020-01-10T22:53:00+00:00\",\"dateModified\":\"2024-09-13T15:04:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/01\\\/10\\\/javascript-tutorial-series-arrays\\\/\"},\"wordCount\":334,\"commentCount\":0,\"articleSection\":[\"javascript\",\"javascript tutorials\",\"tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/01\\\/10\\\/javascript-tutorial-series-arrays\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/01\\\/10\\\/javascript-tutorial-series-arrays\\\/\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/01\\\/10\\\/javascript-tutorial-series-arrays\\\/\",\"name\":\"JavaScript Tutorial Series - Arrays - Zofxare Blog Home\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\"},\"datePublished\":\"2020-01-10T22:53:00+00:00\",\"dateModified\":\"2024-09-13T15:04:52+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/01\\\/10\\\/javascript-tutorial-series-arrays\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/01\\\/10\\\/javascript-tutorial-series-arrays\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/2020\\\/01\\\/10\\\/javascript-tutorial-series-arrays\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Tutorial Series &#8211; Arrays\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/\",\"name\":\"Zofxare Blog Home\",\"description\":\"The Zofxare blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/#\\\/schema\\\/person\\\/488c98b837076eb349075c14ea0e87d8\",\"name\":\"adminuser\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g\",\"caption\":\"adminuser\"},\"sameAs\":[\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\"],\"url\":\"https:\\\/\\\/zofxare.com\\\/zofxare\\\/blog\\\/author\\\/adminuser\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Tutorial Series - Arrays - Zofxare Blog Home","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:\/\/zofxare.com\/zofxare\/blog\/2020\/01\/10\/javascript-tutorial-series-arrays\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Tutorial Series - Arrays - Zofxare Blog Home","og_description":"In JavaScript, arrays store a list of items. An item may be a primitive such as a string or number. An item may also be an object or another array. Indexing starts at 0. Arrays are objects. To determine if a variable is an array, use Object.prototype.toString.call(theArray) === '[object Array]' or the newer way of [&hellip;]","og_url":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/01\/10\/javascript-tutorial-series-arrays\/","og_site_name":"Zofxare Blog Home","article_published_time":"2020-01-10T22:53:00+00:00","article_modified_time":"2024-09-13T15:04:52+00:00","author":"adminuser","twitter_card":"summary_large_image","twitter_misc":{"Written by":"adminuser","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/01\/10\/javascript-tutorial-series-arrays\/#article","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/01\/10\/javascript-tutorial-series-arrays\/"},"author":{"name":"adminuser","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8"},"headline":"JavaScript Tutorial Series &#8211; Arrays","datePublished":"2020-01-10T22:53:00+00:00","dateModified":"2024-09-13T15:04:52+00:00","mainEntityOfPage":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/01\/10\/javascript-tutorial-series-arrays\/"},"wordCount":334,"commentCount":0,"articleSection":["javascript","javascript tutorials","tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/zofxare.com\/zofxare\/blog\/2020\/01\/10\/javascript-tutorial-series-arrays\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/01\/10\/javascript-tutorial-series-arrays\/","url":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/01\/10\/javascript-tutorial-series-arrays\/","name":"JavaScript Tutorial Series - Arrays - Zofxare Blog Home","isPartOf":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website"},"datePublished":"2020-01-10T22:53:00+00:00","dateModified":"2024-09-13T15:04:52+00:00","author":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8"},"breadcrumb":{"@id":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/01\/10\/javascript-tutorial-series-arrays\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/zofxare.com\/zofxare\/blog\/2020\/01\/10\/javascript-tutorial-series-arrays\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/zofxare.com\/zofxare\/blog\/2020\/01\/10\/javascript-tutorial-series-arrays\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/zofxare.com\/zofxare\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript Tutorial Series &#8211; Arrays"}]},{"@type":"WebSite","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#website","url":"https:\/\/zofxare.com\/zofxare\/blog\/","name":"Zofxare Blog Home","description":"The Zofxare blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/zofxare.com\/zofxare\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/zofxare.com\/zofxare\/blog\/#\/schema\/person\/488c98b837076eb349075c14ea0e87d8","name":"adminuser","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c5fe9cf8bb25011247f8063d1c50de6fbdd21be02889559dd151d722f050f037?s=96&d=retro&r=g","caption":"adminuser"},"sameAs":["https:\/\/zofxare.com\/zofxare\/blog"],"url":"https:\/\/zofxare.com\/zofxare\/blog\/author\/adminuser\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/206","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/comments?post=206"}],"version-history":[{"count":2,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/206\/revisions"}],"predecessor-version":[{"id":257,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/posts\/206\/revisions\/257"}],"wp:attachment":[{"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/media?parent=206"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/categories?post=206"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zofxare.com\/zofxare\/blog\/wp-json\/wp\/v2\/tags?post=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}