{"id":1151,"date":"2021-07-02T10:05:24","date_gmt":"2021-07-02T10:05:24","guid":{"rendered":"https:\/\/usemynotes.com\/?p=1151"},"modified":"2023-06-14T15:48:11","modified_gmt":"2023-06-14T15:48:11","slug":"arrays-in-javascript","status":"publish","type":"post","link":"https:\/\/usemynotes.com\/arrays-in-javascript\/","title":{"rendered":"How to create an arrays in JavaScript?"},"content":{"rendered":"<p>Hi, everyone, in this module, we will be learning <strong>Arrays in JavaScript<\/strong>. So far we have learned variables, data types, conditional statements, break and continue statements, and strings in JavaScript. Now, it\u2019s time to know more about arrays.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/usemynotes.com\/arrays-in-javascript\/#Arrays_in_JavaScript\" >Arrays in JavaScript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/usemynotes.com\/arrays-in-javascript\/#How_to_create_an_array_in_JavaScript\" >How to create an array in JavaScript?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/usemynotes.com\/arrays-in-javascript\/#Why_should_we_use_arrays_in_JavaScript\" >Why should we use arrays in JavaScript?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/usemynotes.com\/arrays-in-javascript\/#How_to_access_array_elements_in_JavaScript\" >How to access array elements in JavaScript?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/usemynotes.com\/arrays-in-javascript\/#Updating_an_element_in_an_array_in_JavaScript\" >Updating an element in an array in JavaScript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/usemynotes.com\/arrays-in-javascript\/#Deleting_an_element_in_an_array_in_JavaScript\" >Deleting an element in an array in JavaScript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/usemynotes.com\/arrays-in-javascript\/#Length_property_in_an_array_in_JavaScript\" >Length property in an array in JavaScript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/usemynotes.com\/arrays-in-javascript\/#Array_Methods_in_JavaScript_in_JavaScript\" >Array Methods in JavaScript in JavaScript<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/usemynotes.com\/arrays-in-javascript\/#Multi-dimensional_array_in_JavaScript\" >Multi-dimensional array in JavaScript<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Arrays_in_JavaScript\"><\/span>Arrays in JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In JavaScript, an array is a user-defined or non-primitive data type used to store a collection of elements in a single variable.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-1158 size-full\" src=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/Array-in-Javascript.jpg\" alt=\"Arrays in JavaScript\" width=\"1000\" height=\"700\" title=\"\" srcset=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/Array-in-Javascript.jpg 1000w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/Array-in-Javascript-300x210.jpg 300w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/Array-in-Javascript-768x538.jpg 768w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/Array-in-Javascript-150x105.jpg 150w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/Array-in-Javascript-696x487.jpg 696w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/Array-in-Javascript-600x420.jpg 600w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/Array-in-Javascript-100x70.jpg 100w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/p>\n<p>Arrays in other programming languages like C, C++, and <a href=\"https:\/\/usemynotes.com\/java-programming\/\">Java<\/a> are used to store the same type of elements, but in <a href=\"https:\/\/usemynotes.com\/javascript\/\">JavaScript<\/a>, an array can be used to store both the same and different types of elements i.e., you can store elements in the form of a number, string, Boolean, a character in a single array.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_create_an_array_in_JavaScript\"><\/span>How to create an array in JavaScript?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In JavaScript, an array can be created in two ways:<\/p>\n<ol>\n<li><strong>Using Array literal<\/strong><br \/>\n<strong>Syntax<\/strong><br \/>\nvar variable_name=[element1, element2,\u2026\u2026., element n];<br \/>\n<strong>Example<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar num=&#x5B;1,4.5,true,&#039;Rahul&#039;,null,undefined];\r\nconsole.log(num);\r\n\/\/ Output\r\n\/\/&#x5B;1, 4.5, true, &quot;Rahul&quot;, null, undefined]\r\n<\/pre>\n<\/li>\n<li><strong>Using a new keyword<\/strong><br \/>\n<strong>Syntax<\/strong><br \/>\nvar variable_name=new Array(param1,param2,\u2026..);<br \/>\nwhere arg1, arg2,\u2026. is a parameter list, which is optional.<br \/>\n<strong>Example<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar a=new Array();\r\na&#x5B;0]=2;\r\na&#x5B;3]=1;\r\na&#x5B;1]=4;\r\nconsole.log(a);\r\nvar b=new Array(2.2,1,&#039;A&#039;,&quot;JavaScript&quot;);\r\nconsole.log(b);\r\n\r\n\/\/ Output\r\n\/\/ &#x5B;2, 4, empty, 1]\r\n\/\/ &#x5B;2.2, 1, &quot;A&quot;, &quot;JavaScript&quot;]\r\n<\/pre>\n<\/li>\n<\/ol>\n<p>The first <a href=\"https:\/\/usemynotes.com\/string-and-methods-in-javascript\/\">method<\/a> is mostly used because it is simple to create and fast execution when compared to creating an array using a new keyword.<\/p>\n<p>If we check the <a href=\"https:\/\/usemynotes.com\/what-are-data-types-in-javascript\/\">data type<\/a> of the array, we will get an object as an output.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar num=&#x5B;1,4.5,true,&#039;Rahul&#039;,null,undefined];\r\nconsole.log(typeof num);\r\n\r\n\/\/Output\r\n\/\/object\r\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Why_should_we_use_arrays_in_JavaScript\"><\/span>Why should we use arrays in JavaScript?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Suppose you want to store 50 names. You can simply store it in a variable if the number of names is less, say 10, but to store 50 names, making 50 variables for each name is a very tedious and tough task. It also increases the complexity of code and program length. So, a solution is to store all the students\u2019 names in a single variable. Thus, an array is required to store or hold multiple values at a time.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_access_array_elements_in_JavaScript\"><\/span>How to access array elements in JavaScript?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The elements of the array can be accessed using its index number. The index position of the array starts from 0 and ends at the length of an array-1.<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-1159 aligncenter\" src=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/How-to-access-array-elements.jpg\" alt=\"How to access array elements\" width=\"1000\" height=\"251\" title=\"\" srcset=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/How-to-access-array-elements.jpg 1000w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/How-to-access-array-elements-300x75.jpg 300w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/How-to-access-array-elements-768x193.jpg 768w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/How-to-access-array-elements-150x38.jpg 150w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/How-to-access-array-elements-696x175.jpg 696w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nlet name=&#x5B;&#039;Sneha&#039;,&#039;Piyush&#039;,&#039;Aman&#039;,&#039;Om&#039;];\r\nconsole.log(name&#x5B;0]);\r\nconsole.log(name&#x5B;2]);\r\n\/\/Output\r\n\/\/Sneha\r\n\/\/Aman\r\n\r\n\/\/If we exceed the index position of the array, we will get undefined as an output.\r\nconsole.log(name&#x5B;4]);\r\n\r\n\/\/ Output\r\n\/\/ undefined\r\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Updating_an_element_in_an_array_in_JavaScript\"><\/span>Updating an element in an array in JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar num=&#x5B;13,26,21,10,6,9];\r\nconsole.log(num);\r\nnum&#x5B;2]=-43;\r\nnum&#x5B;0]=3;\r\nconsole.log(num);\r\n\r\n\/\/Output\r\n\/\/&#x5B;13, 26, 21, 10, 6, 9]\r\n\/\/ &#x5B;3, 26, -43, 10, 6, 9]\r\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Deleting_an_element_in_an_array_in_JavaScript\"><\/span>Deleting an element in an array in JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar num=&#x5B;13,26,21,10,6,9];\r\nconsole.log(num);\r\ndelete num&#x5B;2];\r\nconsole.log(num);\r\n\r\n\/\/Output\r\n\/\/&#x5B;13, 26, 21, 10, 6, 9]\r\n\/\/&#x5B;13, 26, empty, 10, 6, 9]\r\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Length_property_in_an_array_in_JavaScript\"><\/span>Length property in an array in JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The length property is used to return the length of an array.<br \/>\n<strong>Syntax<\/strong><br \/>\nArray.length<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nlet name=&#x5B;&#039;Sneha&#039;,&#039;Piyush&#039;,&#039;Aman&#039;,&#039;Om&#039;];\r\nconsole.log(name.length);\r\n\r\n\/\/Output\r\n\/\/4\r\n<\/pre>\n<p>We can use the length property to print the last element of an array.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nlet len=name.length;\r\nconsole.log(name&#x5B;len-1]);\r\n\r\n\/\/Output\r\n\/\/Om\r\n<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Array_Methods_in_JavaScript_in_JavaScript\"><\/span>Array Methods in JavaScript in JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ol>\n<li><strong>sort<\/strong>()<br \/>\nIt is an array method used to sort the elements of the array in ascending or increasing order.<br \/>\n<strong>Syntax<\/strong><br \/>\narray.sort();<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst a=&#x5B;4,2,1,3,5];\r\nconst b=&#x5B;&#039;JavaScript&#039;,&quot;AngularJS&quot;,&quot;ReactJs&quot;,&quot;MongoDB&quot;];\r\nconsole.log(a.sort());\r\nconsole.log(b.sort());\r\n\r\n\/\/Output\r\n\/\/&#x5B;1, 2, 3, 4, 5]\r\n\/\/&#x5B;&quot;AngularJS&quot;, &quot;JavaScript&quot;, &quot;MongoDB&quot;, &quot;ReactJs&quot;]\r\n<\/pre>\n<\/li>\n<li><strong>reverse<\/strong>()<br \/>\nIt is a method used to reverse the elements of an array.<br \/>\n<strong>Syntax<\/strong><br \/>\narray.reverse();<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst a=&#x5B;4,2,1,3,5];\r\nconsole.log(a);\r\nconsole.log(a.reverse());\r\n\r\n\/\/Output\r\n\/\/&#x5B;4, 2, 1, 3, 5]\r\n\/\/&#x5B;5, 3, 1, 2, 4]\r\n<\/pre>\n<\/li>\n<li><strong>pop<\/strong>()<br \/>\nIt is a method used to remove and return the last element of an array. If the array is empty, undefined is returned as an output.<br \/>\n<strong>Syntax<\/strong><br \/>\narray.pop();<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst a=&#x5B;4,2,1,3,5];\r\nconsole.log(a);\r\nlet b=a.pop();\r\nconsole.log(b);\r\n\r\n\/\/Output\r\n\/\/&#x5B;4, 2, 1, 3, 5]\r\n\/\/5\r\n<\/pre>\n<\/li>\n<li><strong>push<\/strong>()<br \/>\nIt is an array method used to insert an element at the end of an array.<br \/>\n<strong>Syntax<\/strong><br \/>\narray.push(element);<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst a=&#x5B;4,2,1,3,5];\r\nconsole.log(a);\r\na.push(0);\r\nconsole.log(a);\r\n\r\n\/\/Output\r\n\/\/&#x5B;4, 2, 1, 3, 5]\r\n\/\/&#x5B;4, 2, 1, 3, 5, 0]\r\n<\/pre>\n<\/li>\n<li><strong>shift<\/strong>()<br \/>\nIt is used to remove or delete and return the first element from the array.<br \/>\n<strong>Syntax<\/strong><br \/>\narray.shift();<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst a=&#x5B;4,2,1,3,5];\r\nconsole.log(a);\r\nlet b=a.shift();\r\nconsole.log(b);\r\nconsole.log(a);\r\n\r\n\/\/Output\r\n\/\/&#x5B;4, 2, 1, 3, 5]\r\n\/\/4\r\n\/\/&#x5B;2, 1, 3, 5]\r\n<\/pre>\n<\/li>\n<li><strong>unshift<\/strong>()<br \/>\nIt is used to add or insert and return the first element from the array.<br \/>\n<strong>Syntax<\/strong><br \/>\narray.unshift(element);<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconst a=&#x5B;4,2,1,3,5,0];\r\nconsole.log(a);\r\na.unshift(6);\r\nconsole.log(a);\r\n\r\n\/\/Output\r\n\/\/&#x5B;4, 2, 1, 3, 5, 0]\r\n\/\/&#x5B;6, 4, 2, 1, 3, 5, 0]\r\n<\/pre>\n<\/li>\n<li><strong>concat<\/strong>()<br \/>\nIt is a method used to concatenate two or more arrays.<br \/>\n<strong>Syntax<\/strong><br \/>\narray.concat(arr1,arr2,\u2026);<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar a=&#x5B;1,4];\r\nlet b=&#x5B;&quot;C&quot;,&#039;Komal&#039;,5];\r\nlet c=a.concat(&#x5B;3,6]);\r\nconsole.log(c);\r\nlet d=a.concat(a,b);\r\nconsole.log(d);\r\n\r\n\/\/Output\r\n\/\/&#x5B;1, 4, 3, 6]\r\n\/\/&#x5B;1, 4, 1, 4, &quot;C&quot;, &quot;Komal&quot;, 5]\r\n<\/pre>\n<\/li>\n<li><strong>join<\/strong>()<br \/>\nIt is used to convert multiple values of an array into a single string value.<br \/>\n<strong>Syntax<\/strong><br \/>\narray.join(separator)<br \/>\nThe separator is optional.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar c=&#x5B;&#039;p&#039;,&#039;h&#039;,&#039;p&#039;];\r\nlet a=c.join();\r\nlet b=c.join(&#039;\/&#039;)\r\nconsole.log(a);\r\nconsole.log(typeof(a));\r\nconsole.log(b);\r\n\r\n\/\/Output\r\n\/\/p,h,p\r\n\/\/string\r\n\/\/p\/h\/p\r\n<\/pre>\n<\/li>\n<li><strong>includes<\/strong>()<br \/>\nThis array method is used to return true if the given element is found in an array, otherwise, it returns false.<br \/>\n<strong>Syntax<\/strong><br \/>\narray.includes(element);<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nlet a=&#x5B;3,&quot;Raman&quot;,8.9,0];\r\nconsole.log(a.includes(&quot;Raman&quot;));\r\nconsole.log(a.includes(8));\r\nconsole.log(a.includes(3,1));\r\n\r\n\/\/Output\r\n\/\/true\r\n\/\/false\r\n\/\/false\r\n<\/pre>\n<\/li>\n<li><strong>find<\/strong>()<br \/>\nThis array method is used to search and return the first element in an array that satisfies a given condition.<br \/>\n<strong>Syntax<\/strong><br \/>\narray.find(function_name);<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nlet num=&#x5B;19,23,12,8,3,6,4];\r\nlet greater=num.find(element =&gt; {\r\nreturn element&gt;=15;\r\n});\r\nconsole.log(greater);\r\n\r\n\/\/ Output\r\n\/\/19\r\n<\/pre>\n<\/li>\n<li><strong>filter<\/strong>()<br \/>\nIt is used to return a new array based upon a given condition.<br \/>\n<strong>Syntax<\/strong><br \/>\narray.filter(function_name);<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction check_even(element)\r\n{\r\nreturn element%2==0;\r\n}\r\nlet num=&#x5B;19,23,12,8,3,6,4];\r\nlet even=num.filter(check_even);\r\nconsole.log(even);\r\n\r\n\/\/Output\r\n\/\/&#x5B;12, 8, 6, 4]The above example can be done using arrow function.\r\nlet num=&#x5B;19,23,12,8,3,6,4];\r\nlet even=num.filter(element =&gt; {\r\nreturn element%2==0;\r\n});\r\nconsole.log(even);\r\n<\/pre>\n<\/li>\n<li><strong>indexOf<\/strong>()<br \/>\nIt returns the index of the first occurrence of a given element in an array if found, otherwise, it returns -1.<br \/>\n<strong>Syntax<\/strong><br \/>\narray.indexOf(element,start);<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nThe start is the starting index where searching starts and is optional.let num=&#x5B;19,23,12,8,3,6,4];\r\nconsole.log(num.indexOf(8));\r\nconsole.log(num.indexOf(5));\r\nconsole.log(num.indexOf(6,2));\r\n\r\n\/\/Output\r\n\/\/3\r\n\/\/-1\r\n\/\/5\r\n<\/pre>\n<\/li>\n<li><strong>lastIndexOf<\/strong>()<br \/>\nIt returns the index of the last occurrence of a given element in an array if found, otherwise, it returns -1.<br \/>\n<strong>Syntax<\/strong><br \/>\narray.lastIndexOf(element,index)<br \/>\nThe start is a starting index where searching starts from backward and is optional.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nlet num=&#x5B;15,2,10,8,3,2,4];\r\nconsole.log(num.lastIndexOf(2));\r\nconsole.log(num.lastIndexOf(5));\r\nconsole.log(num.lastIndexOf(10,4));\r\n\r\n\/\/Output\r\n\/\/5\r\n\/\/-1\r\n\/\/2\r\n<\/pre>\n<\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"Multi-dimensional_array_in_JavaScript\"><\/span>Multi-dimensional array in JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>A multi-dimensional array is an array within another array. We can create a 2-D array, a 3-D array, and so on.<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-1160 aligncenter\" src=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/Multi-dimensional-array.jpg\" alt=\"Multi-dimensional array\" width=\"1000\" height=\"700\" title=\"\" srcset=\"https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/Multi-dimensional-array.jpg 1000w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/Multi-dimensional-array-300x210.jpg 300w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/Multi-dimensional-array-768x538.jpg 768w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/Multi-dimensional-array-150x105.jpg 150w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/Multi-dimensional-array-696x487.jpg 696w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/Multi-dimensional-array-600x420.jpg 600w, https:\/\/usemynotes.com\/wp-content\/uploads\/2021\/07\/Multi-dimensional-array-100x70.jpg 100w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/p>\n<p>In a 2-D or 2-dimensional array, elements are organized in the form of a matrix. There are several rows and columns in it.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar info=&#x5B;&#x5B;&quot;Aman&quot;,21,&quot;BCA&quot;],&#x5B;&quot;Akash&quot;,20,&quot;BA&quot;],&#x5B;&quot;Sneha&quot;,21,&quot;Btech&quot;]];\r\nconsole.log(info);\r\n<\/pre>\n<table>\n<tbody>\n<tr>\n<td>(index)<\/td>\n<td>0<\/td>\n<td>1<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>&#8216;Aman&#8217;<\/td>\n<td>21<\/td>\n<td>&#8216;BCA&#8217;<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>&#8216;Akash&#8217;<\/td>\n<td>20<\/td>\n<td>&#8216;BA&#8217;<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>&#8216;Sneha&#8217;<\/td>\n<td>21<\/td>\n<td>&#8216;BSc&#8217;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In a 2-D array, we need two indices, one for the row and the other for the column, to access an element.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nconsole.log(info&#x5B;0]&#x5B;0]);\r\n\r\n\/\/ Output\r\n\/\/ Aman\r\n<\/pre>\n<p>info[0][0] means that the element at the 0th row and 0th column is accessed. Similarly, you can access all the other elements of the 2-D array.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar info=&#x5B;&#x5B;&quot;Aman&quot;,21,&quot;BCA&quot;],&#x5B;&quot;Akash&quot;,20,&quot;BA&quot;],&#x5B;&quot;Sneha&quot;,21,&quot;Btech&quot;]];\r\nfor(let i=0;i&lt;info.length;i++)\r\n{\r\nfor(let j=0;j&lt;info&#x5B;i].length;j++)\r\nconsole.log((j+1),info&#x5B;i]&#x5B;j]);\r\nconsole.log();\r\n}\r\n\r\n\/\/ Output\r\n\/\/1 Aman\r\n\/\/2 21\r\n\/\/3 BCA\r\n\r\n\/\/1 Akash\r\n\/\/2 20\r\n\/\/3 BA\r\n\r\n\/\/1 Sneha\r\n\/\/2 21\r\n\/\/3 Btech\r\n<\/pre>\n<p>I hope this module has helped you a lot in knowing <strong>arrays in JavaScript<\/strong>. You might be excited to implement the same by yourself to gain practical knowledge. Stay tuned with us for more modules like this. Keep Coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, everyone, in this module, we will be learning Arrays in JavaScript. So far we have learned variables, data types, conditional statements, break and continue statements, and strings in JavaScript. Now, it\u2019s time to know more about arrays. Arrays in JavaScript In JavaScript, an array is a user-defined or non-primitive data type used to store [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1158,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":{"0":"post-1151","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript"},"_links":{"self":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts\/1151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/comments?post=1151"}],"version-history":[{"count":0,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/posts\/1151\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/media\/1158"}],"wp:attachment":[{"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/media?parent=1151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/categories?post=1151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/usemynotes.com\/wp-json\/wp\/v2\/tags?post=1151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}