{"id":4471,"date":"2016-03-02T11:48:06","date_gmt":"2016-03-02T11:48:06","guid":{"rendered":"http:\/\/www.hostingnotes.com\/?p=4471"},"modified":"2017-10-09T14:34:13","modified_gmt":"2017-10-09T14:34:13","slug":"javascript-arrays-creating-and-storing-values","status":"publish","type":"post","link":"https:\/\/www.webdevelopersnotes.com\/javascript-arrays-creating-and-storing-values","title":{"rendered":"JavaScript Arrays &#8211; creating and storing values"},"content":{"rendered":"<p>An array can be regarded as a set of variables. This does not mean that the variables in an array are related to each other; they are still separate entities in themselves. Arrays helps us group variables, which we plan to use for a particular purpose because accessing their values becomes a lot easier this way.<\/p>\n<p>Consider the following table:<\/p>\n<table class=\"smalltext\">\n<tr>\n<th><strong>Number<\/strong><\/th>\n<th><strong>City name<\/strong><\/th>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>New York<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>London<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>New Delhi<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>Sydney<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>Tokyo<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>Moscow<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>Lima<\/td>\n<\/tr>\n<\/table>\n<p>To store these city names, we can either employ different variables such as city1, city2, city3 &#8230; or insert these values into an array. Let&#8217;s see how we make an array.<\/p>\n<pre class=\"small-text\">\r\nvar city = new Array();\r\n<\/pre>\n<p>Each array is initialised using the <em>new<\/em> keyword with the <em>Array()<\/em> construct (Isn&#8217;t this similar to using <em>Date()<\/em>?). So here we initialise and array called <strong>city<\/strong>. Now we fill this array with our values (city names).<\/p>\n<pre class=\"small-text\">\r\ncity[0] = \"New York\";\r\ncity[1] = \"London\";\r\ncity[2] = \"New Delhi\";\r\ncity[3] = \"Sydney\";\r\ncity[4] = \"Tokyo\";\r\ncity[5] = \"Moscow\";\r\ncity[6] = \"Lima\";\r\n<\/pre>\n<p>Each variable in an array begins with the array name followed by a numeric value contained in a pair of square brackets. This numeric value signifies the <em><strong>Index Number<\/strong><\/em> of that variable in the array. Note, that arrays are zero-indexed, that is, they start at index value 0. Also, we don&#8217;t need the <em>var<\/em> keyword to initialise individual variables of an array.<\/p>\n<p>JavaScript allows us to write the array initialisation and assignment statements as one. Thus,<\/p>\n<pre class=\"small-text\">\r\nvar city = new Array(\"New York\", \"London\", \"New Delhi\", \r\n\"Sydney\", \"Tokyo\", \"Moscow\", \"Lima\");\r\n<\/pre>\n<p>Array values are accessed through their index names. We can use a <em>for<\/em> loop for this purpose.<\/p>\n<pre class=\"small-text\">\r\nvar msg = \"\";\r\nfor (x = 0; x &lt; 7; x++)\r\n   {\r\n   msg = msg + city[x] + \"\\n\";;\r\n   }\r\nalert(msg);\r\n<\/pre>\n<p><a href=\"javascript:void(0)\" onclick=\"var city = new Array('New York', 'London', 'New Delhi', 'Sydney', 'Tokyo', 'Moscow', 'Lima');var msg = '';for (x = 0; x &lt; 7; x++){msg = msg + city[x] + '\\n';}alert(msg);\">Click here for the result<\/a>.<\/p>\n<p>Now, don&#8217;t you see the usefulness of arrays? If we had the values in variables of different names (even city1, city2&#8230;), our <em>alert()<\/em> statement would be very long.<\/p>\n<p>Values stored in arrays can be changed just like other variable values. Further, you can increase or decrease the number of items in an array&#8230; but that&#8217;s for laters.<\/p>\n<p>The length of an array is found though its <em><strong>length<\/strong><\/em> property. The code below, finds the length of the array, adds 50 to each value and displays the result in an alert box.<\/p>\n<pre class=\"small-text\">\r\nvar num = new Array(200, 400, 300, 250, 670, 430, 220, 870, 30);\r\n<strong>var l = num.length<\/strong>\r\nvar msg = \"\";\r\nfor (x = 0; x &lt; l; x++)\r\n   {\r\n   <strong>num[x] = num[x] + 50;<\/strong>\r\n   msg = msg + num[x]+ \"\\n\";\r\n   }\r\nalert(msg);\r\n<\/pre>\n<p><a href=\"javascript:void(0)\" onclick=\"var num = new Array(200, 400, 300, 250, 670, 430, 220, 870, 30);var l = num.length;var msg = '';for (x = 0; x &lt; l; x++){num[x] = num[x] + 50;msg = msg + num[x] + '\\n';}alert(msg);\">Click here for the result<\/a>.<\/p>\n<p>After initialising the array <strong>num<\/strong> and asigning values to it, we find its length and store the value in variable <strong>l<\/strong>. The variable is then used in a <em>for<\/em> loop where each array value is incremented by 50.<\/p>\n<p>Lastly, you can mix the data types in an array which means that in a single array, some variables can contain string while the other might contain numeric data and JavaScript wouldn&#8217;t complain!<\/p>\n<div class=\"pagenav\">\n<ul>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/generating-random-numbers-in-javascript\" title=\"Generating random numbers in javascript\">&lt; Back<\/a><\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-tutorial\" title=\"JavaScript tutorial\">1<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-event-handlers-part-1\" title=\"Javascript onmouseover and javascript onmouseout event handlers\">10<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-if-statement\" title=\"Javascript IF statement\">20<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/changing-images-on-mouseover-using-javascript\" title=\"Changing images on mouseover using Javascript\">30<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/javascript-arrays-creating-and-storing-values\" title=\"Creating and storing values in Javascript arrays\" class=\"thispage\">33<\/a><\/li>\n<li>&#8230;<\/li>\n<li><a href=\"\/\/www.webdevelopersnotes.com\/random-text-display-using-javascript\" title=\"random text display using javascript\">Next &gt;<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"An array can be regarded as a set of variables. This does not mean that the variables in an array are related to each other; they are still separate entities in themselves. Arrays helps us group variables, which we plan to use for a particular purpose because accessing their values becomes a lot easier this [&hellip;]","protected":false},"author":1,"featured_media":15638,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[56],"tags":[],"class_list":["post-4471","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/posts\/4471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/comments?post=4471"}],"version-history":[{"count":0,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/posts\/4471\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/media\/15638"}],"wp:attachment":[{"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/media?parent=4471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/categories?post=4471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webdevelopersnotes.com\/wp-json\/wp\/v2\/tags?post=4471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}