{"flag":true,"single":true,"pageTitle":"Arrays In Javascript","post":{"id":204,"user_id":"1","slug":"arrays-in-javascript-ehpu","title":"Arrays In Javascript","body":"<p>var cars = [\"Saab\", \"Volvo\", \"BMW\"];<\/p>\r\n<p>var name = cars[0]; <strong>\/\/ACCESS ARRAY ELEMENT saab<\/strong><\/p>\r\n<p>cars[0] = \"Opel\";&nbsp; <strong>\/\/CHANGE ARRAY<\/strong><\/p>\r\n<p>console.log(cars); <strong>\/\/Saab,volvo,BMW \/\/FULL ARRAY ACCESS<\/strong><\/p>\r\n<p>cars.toString(); <strong>\/\/ARRAY TO STRING<\/strong><\/p>\r\n<p>&nbsp;<\/p>\r\n<p>var fruits = [\"Banana\", \"Orange\", \"Apple\", \"Mango\"];<\/p>\r\n<p>fruits.pop();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>\/\/ Removes the last element (\"Mango\") from fruits and return also<\/strong><\/p>\r\n<p>fruits.push(\"Kiwi\");&nbsp; &nbsp;<strong> &nbsp; &nbsp;\/\/&nbsp; Adds a new element (\"Kiwi\") to fruits and return new array length<\/strong><\/p>\r\n<p>fruits.shift();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <strong>\/\/ Removes the first element \"Banana\" from fruits same as pop<\/strong><\/p>\r\n<p>fruits.unshift(\"Lemon\");&nbsp; <strong>&nbsp; \/\/ Adds a new element \"Lemon\" to fruits at first pos<\/strong><\/p>\r\n<p>delete fruits[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<strong>\/\/ Changes the first element in fruits to undefined<\/strong><\/p>\r\n<p>fruits.splice(2, 2, \"Lemon\", \"Kiwi\");&nbsp;<strong> \/\/fruits.splice(whereNewElementsAdds,removedElements, \"add1element\", \"add2 ...\");<\/strong><\/p>\r\n<p>var marr = arr1.concat(arr2,arr3,arr4);&nbsp;<strong> \/\/concate arrays to new arr<\/strong><\/p>\r\n<p>fruits.sort();&nbsp; &nbsp; &nbsp; <strong>&nbsp; \/\/ Sorts the elements of fruits alphabetically<\/strong><\/p>\r\n<p>fruits.reverse();<strong>&nbsp; &nbsp; &nbsp;\/\/ Then reverse the order of the elements<\/strong><\/p>\r\n<p>var points = [40, 100, 1, 5, 25, 10];<\/p>\r\n<p>points.sort(function(a, b){return a - b});&nbsp; <strong>\/\/SORT NUMERIC ARRAY ACCENDING<\/strong><\/p>\r\n<p><strong>ARRAY ITERATION<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>var txt=\"\";\r\nvar numbers = [45, 4, 9, 16, 25];\r\nnumbers.forEach(myFunction);\r\nfunction myFunction(value, index, array) {\r\n  txt = txt + value + \"&lt;br&gt;\";\r\n}<\/code><\/pre>\r\n<p><strong>ARRAY MAP CREATE NEW ARRAY: to perform function on each element<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>var numbers1 = [45, 4, 9, 16, 25];\r\nvar numbers2 = numbers1.map(myFunction);\r\nfunction myFunction(value, index, array) {\r\n  return value * 2;\r\n}<\/code><\/pre>\r\n<p><strong>ARRAY FILTER: to perform tests<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>var numbers = [45, 4, 9, 16, 25];\r\nvar over18 = numbers.filter(myFunction);\r\nfunction myFunction(value, index, array) {\r\n  return value &gt; 18;\r\n}\r\n\r\n<\/code><\/pre>\r\n<p>array.indexOf(item, start)<\/p>\r\n<p>array.lastIndexOf(item, start)<\/p>","category_id":"12","is_private":"0","created_at":"2023-10-13T01:32:59.000000Z","updated_at":"2023-10-13T01:32:59.000000Z","category":{"id":12,"user_id":"1","name":"Javascript","slug":"javascript-xplw","parent_id":null,"created_at":"2023-03-23T02:24:43.000000Z","updated_at":"2023-03-23T02:24:43.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"var cars = [\"Saab\", \"Volvo\", \"BMW\"]; var name = cars[0]; \/\/ACCESS ARRAY ELEMENT saab cars[0] = \"Opel\";&nbsp; \/\/CHANGE ARRAY console.log(cars - Arrays In Javascript (Updated: October 13, 2023) - Read more about Arrays In Javascript at my programming site [SITE]","categories":[]}