{"id":368,"date":"2020-08-20T19:00:26","date_gmt":"2020-08-20T19:00:26","guid":{"rendered":"https:\/\/datatype.co.in\/blog\/?p=368"},"modified":"2020-09-04T19:19:47","modified_gmt":"2020-09-04T19:19:47","slug":"important-javascript-array-methods","status":"publish","type":"post","link":"https:\/\/datatype.co.in\/blog\/important-javascript-array-methods\/","title":{"rendered":"Important JavaScript Array Methods"},"content":{"rendered":"\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/js-array-methods-1024x576.png\" alt=\"Important JavaScript Array Methods\" class=\"wp-image-843\" srcset=\"https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/js-array-methods-1024x576.png 1024w, https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/js-array-methods-300x169.png 300w, https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/js-array-methods-768x432.png 768w, https:\/\/datatype.co.in\/blog\/wp-content\/uploads\/2020\/09\/js-array-methods-710x399.png 710w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In this article I will discuss about some of the  important inbuilt array methods in JavaScript. I will cover the following methods:<\/p>\n\n\n\n<p><code>length, concat, sort, reverse, split, join, push, pop, unshift, shift, Array.isArray, forEach, map, filter<\/code><\/p>\n\n\n\n<p><strong>length:<\/strong><\/p>\n\n\n\n<p>The <strong><code>length()<\/code><\/strong> methods returns the length of an array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var fruits = [\"Orange\", \"Apple\", \"Banana\"];\nconsole.log(fruits.length) \/\/ Output: 3<\/code><\/pre>\n\n\n\n<p>While dealing with arrays in programming, often we come across a situation where we need to check if the given array is empty or not. The length method can be used for the same. If the length of the array is zero, then it is empty.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var arr = [];\nconsole.log(arr.length) \/\/ Output: 0<\/code><\/pre>\n\n\n\n<p><strong>concat:<\/strong><\/p>\n\n\n\n<p>The concat() method is used to merge two or more arrays into single array.  This method does not change the existing arrays, but instead returns a new array as you can see in the example below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var arr1 = [1,2,3];\nvar arr2 = [4,5];\nvar arr3 = arr1.concat(arr2);\nconsole.log(arr3); \/\/ Output: [1,2,3,4,5]\n<\/code><\/pre>\n\n\n\n<p><strong>sort:<\/strong><\/p>\n\n\n\n<p> The&nbsp;<code><strong>sort()<\/strong><\/code>&nbsp;method&nbsp;is used to sort an array elements in JavaScript. It returns sorted array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var fruits = [\"Orange\", \"Apple\", \"Banana\"];\nconsole.log(fruits.sort()); \/\/ Output: [\"Apple\",\"Banana\",\"Orange\"]<\/code><\/pre>\n\n\n\n<p><strong>reverse:<\/strong><\/p>\n\n\n\n<p> The&nbsp;<code><strong>reverse()<\/strong><\/code>&nbsp;method&nbsp;is used to sort an array elements in reverse order in JavaScript. It&nbsp;returns the array&nbsp;after the reversal. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var fruits = [\"Orange\", \"Apple\", \"Banana\"];\nconsole.log(fruits.reverse()); \/\/ Output: [\"Orange\", \"Banana\", \"Apple\"]<\/code><\/pre>\n\n\n\n<p><strong>split:<\/strong><\/p>\n\n\n\n<p>The <strong><code>split()<\/code><\/strong> method splits the string into an array by the given delimiter or separator. For example, you can split a sentence into words array based on space as separator or any comma separated string into array of words.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var str1 = \"JavaScript is awesome!\"\nconsole.log(str1.split(\" \")); \/\/ Output: [\"JavaScript\", \"is\", \"awesome!\"]\n\nvar str2 = \"Jan, Feb, Mar, Apr\";\nconsole.log(str2.split(\", \")); \/\/ Output: [\"Jan\", \"Feb\", \"Mar\", \"Apr\"]<\/code><\/pre>\n\n\n\n<p> The&nbsp;<code>split<\/code>&nbsp;method has an optional second numeric argument \u2013 a limit on the array length. If it is provided, then the extra elements are ignored. In practice it is rarely used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var str2 = \"Jan, Feb, Mar, Apr\";\nconsole.log(str2.split(\", \", 2)); \/\/ Output: [\"Jan\", \"Feb\"]<\/code><\/pre>\n\n\n\n<p><strong>join:<\/strong><\/p>\n\n\n\n<p>The<strong> join() <\/strong>method does opposite of split i.e. it creates string out of array glued by given separator (as argument). If no separator or argument is provided, it returns comma separated string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Without separator\nvar months = [\"Jan\", \"Feb\", \"Mar\", \"Apr\"];\nconsole.log(months.join()); \/\/ Output: Jan,Feb,Mar,Apr\n\n\/\/ With separator\nvar months = [\"Jan\", \"Feb\", \"Mar\", \"Apr\"];\nconsole.log(months.join(\"-\")); \/\/ Output: Jan-Feb-Mar-Apr<\/code><\/pre>\n\n\n\n<p><strong>push and pop:<\/strong><\/p>\n\n\n\n<p>The <strong><code>push()<\/code><\/strong> method is used to add one or more elements <strong>at the end <\/strong>of an array. The element is provided as an argument. It returns array with the pushed element.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var months = [\"Jan\", \"Feb\", \"Mar\", \"Apr\"];\n\/\/ add one element\nmonths.push(\"May\"); \nconsole.log(months); \/\/ Output: [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\"]\n\n\/\/ add multiple elements\nmonths.push(\"Jun\",\"Jul\"); \nconsole.log(months); \/\/ Output: [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\"]<\/code><\/pre>\n\n\n\n<p>The <strong><code>pop()<\/code><\/strong> deletes <strong>last <\/strong>element from an array and returns rest of the array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var months = [\"Jan\", \"Feb\", \"Mar\", \"Apr\"];\nconsole.log(months.pop()); \/\/ Output: [\"Jan\", \"Feb\", \"Mar\"]<\/code><\/pre>\n\n\n\n<p><strong>unshift and shift:<\/strong><\/p>\n\n\n\n<p> The&nbsp;<code><strong>unshift()<\/strong><\/code>&nbsp;method adds one or more elements <strong>to the beginning<\/strong> of an array and returns the new length of the array. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var months = [\"Jan\", \"Feb\", \"Mar\", \"Apr\"];\nconsole.log(months.unshift(\"May\", \"Jun\")); \/\/ Output: 6\nconsole.log(months) \/\/ Output: [\"May\", \"Jun\", \"Jan\", \"Feb\", \"Mar\", \"Apr\"]<\/code><\/pre>\n\n\n\n<p>The&nbsp;<code><strong>shift()<\/strong><\/code>&nbsp;method removes the&nbsp;<strong>first<\/strong>&nbsp;element from an array and returns that removed element. This method reduces the length of the array by one. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var months = [\"Jan\", \"Feb\", \"Mar\", \"Apr\"];\nvar firstElement = months.shift();\nconsole.log(firstElement); \/\/ Output: Jan\nconsole.log(months) \/\/ Output: [\"Feb\", \"Mar\", \"Apr\"]<\/code><\/pre>\n\n\n\n<p><strong>Array.isArray:<\/strong><\/p>\n\n\n\n<p>In the previous article &#8211; <a href=\"https:\/\/datatype.co.in\/blog\/data-types-in-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Data Types in JavaScript<\/a>, we have seen that data type of an <strong><code>Array<\/code><\/strong> in JavaScript is <strong><code>Object<\/code><\/strong>. The <strong><code>typeof <\/code><\/strong>operator does not help is differentiating array from object as it returns <strong><code>Object<\/code><\/strong> on both cases.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var obj = {foo: \"bar\"}; \/\/ Object\nvar arr = [1, 2, 3]; \/\/ Array\n\nconsole.log(typeof obj); \/\/ Output: Object\nconsole.log(typeof arr); \/\/ Output: Object<\/code><\/pre>\n\n\n\n<p> Then how do you check if a given value is array in JavaScript? The answer is: using <strong><code>Array.isArray()<\/code><\/strong> method. <\/p>\n\n\n\n<p>The&nbsp;<code><strong>Array.isArray()<\/strong><\/code>&nbsp;method&nbsp;determines whether the passed value is an&nbsp;<code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\">Array<\/a><\/code> or not.  It accepts value as argument and returns<strong>  <code>true<\/code><\/strong> if the value passed is an array, otherwise it returns <strong><code>false<\/code><\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Array.isArray([1, 2, 3]);  \/\/ Output: true\nArray.isArray({foo: \"bar\"}); \/\/ Output: false\nArray.isArray('Hello');   \/\/ Output: false\nArray.isArray(undefined);  \/\/ Output: false<\/code><\/pre>\n\n\n\n<p><strong>forEach:<\/strong><\/p>\n\n\n\n<p>The <strong><code>forEach()<\/code><\/strong> method is used to loop through an array elements. The loop executes once for each array element.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var months = [\"Jan\", \"Feb\", \"Mar\", \"Apr\"];\nmonths.forEach(function(element){\n   console.log(element);\n})\n\/\/ The loop will iterate four times\n\/\/ Output: \n\"Jan\"\n\"Feb\"\n\"Mar\"\n\"Apr\"<\/code><\/pre>\n\n\n\n<p>Using JavaScript ES6 Arrow Function we can achieve the same with just single line of code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var months = [\"Jan\", \"Feb\", \"Mar\", \"Apr\"];\nmonths.forEach(element => console.log(element));<\/code><\/pre>\n\n\n\n<p><strong>map:<\/strong><\/p>\n\n\n\n<p> The&nbsp;<code><strong>map()<\/strong><\/code>&nbsp; method in JavaScript creates a new array with the results of calling a function for every array element. The provided function is called once for each array element. The <code><strong>map<\/strong><\/code> method does not alter the original array. Note that map method works on all modern browsers except IE.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr1 = [1,2,3,4];\n\/\/ pass a function to map\nconst arr2 = arr1.map(x => x * 2);\nconsole.log(arr2); \/\/ output: Array [2, 4, 6, 8]\n\n\/\/ No change in the original array\nconsole.log(arr1); \/\/ output: Array [1,2,3,4]<\/code><\/pre>\n\n\n\n<p>As you can see, we have passed a multiplier function which multiplies each array elements by 2. <\/p>\n\n\n\n<p><strong>filter:<\/strong><\/p>\n\n\n\n<p>The <strong><code>filter()<\/code><\/strong> method creates a new array with elements that fall under a given criteria from an existing array. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var numbers = [1, 3, 6, 8, 11, 14];\nvar odd = numbers.filter(function(number) {\n  return number % 2 > 0 ; \/\/ Return odd numbers only\n});\nconsole.log(odd);\n\/\/ Output: [ 1, 3, 11 ]<\/code><\/pre>\n\n\n\n<p>In the above example, the filter <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/datatype.co.in\/blog\/what-is-callback-in-javascript\/\" target=\"_blank\">callback function<\/a> checks if the number is odd using the <strong>modulas operator<\/strong> <code style=\"\"><b>(%)<\/b><\/code>. <strong>Modulus<\/strong>&nbsp;operator returns the division remainder. The remainder &gt;0 means the number is odd.<\/p>\n\n\n\n<p>You can try the above examples <a rel=\"noreferrer noopener\" aria-label=\"here (opens in a new tab)\" href=\"https:\/\/datatype.co.in\/code-editor\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<p>You may also read: <br><a href=\"https:\/\/datatype.co.in\/blog\/what-is-callback-in-javascript\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">What is Callback in JavaScript<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article I will discuss about some of the important inbuilt array methods in JavaScript. I will cover the following methods: length, concat, sort, reverse, split, join, push, pop,&nbsp;[ &hellip; ]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[164,206,158,163,152,162,159,160,161,157,100,207],"class_list":["post-368","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-map-in-javascript","tag-filter-in-javascript","tag-javascript-array-length","tag-javascript-array-merge","tag-javascript-array-methods","tag-javascript-array-reverse","tag-javascript-array-shift","tag-javascript-array-sort","tag-javascript-array-unshift","tag-javascript-foreach-loop","tag-js-interview-question","tag-modulus-operator","list-style-post"],"_links":{"self":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/comments?post=368"}],"version-history":[{"count":51,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/368\/revisions"}],"predecessor-version":[{"id":844,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/posts\/368\/revisions\/844"}],"wp:attachment":[{"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/media?parent=368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/categories?post=368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/datatype.co.in\/blog\/wp-json\/wp\/v2\/tags?post=368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}