{"componentChunkName":"component---src-templates-post-js","path":"/javascript-built-in-data-structure-array-part-2-3/","result":{"data":{"markdownRemark":{"html":"<p><em>This tutorial is a part of the <a href=\"/learn-everything-about-javascript-in-one-course/\">Learn everything about Javascript in one course</a>.</em></p>\n<p><em>Javascript built-in data structure - Array, <a href=\"/javascript-built-in-data-structure-array-part-1-3/\">part 1/3</a> part 2/3 (current), <a href=\"/javascript-built-in-data-structure-array-part-3-3/\">part 3/3</a>.</em></p>\n<!-- `youtube:https://www.youtube.com/watch?v=tBdMdNLjtbU` -->\n<h2 id=\"get-length-of-an-array-property-length\" style=\"position:relative;\"><a href=\"#get-length-of-an-array-property-length\" aria-label=\"get length of an array property length permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Get length of an array, property length</h2>\n<p>Syntax <code class=\"language-text\">array.length</code>. Array length is the number of elements in array.</p>\n<div class=\"filename\">array.js</div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// Get length of an array, property length</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'a'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">)</span> <span class=\"token comment\">// 4, length 4</span></code></pre></div>\n<h2 id=\"empty-array-is-different-from-sparse-array\" style=\"position:relative;\"><a href=\"#empty-array-is-different-from-sparse-array\" aria-label=\"empty array is different from sparse array permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Empty array is different from sparse array</h2>\n<table>\n<thead>\n<tr>\n<th>Empty array</th>\n<th>sparse array</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code class=\"language-text\">[]</code></td>\n<td><code class=\"language-text\">[,,,]</code> or <code class=\"language-text\">new Array(0)</code></td>\n</tr>\n<tr>\n<td>array has <strong>no</strong> element</td>\n<td>has empty element</td>\n</tr>\n<tr>\n<td>array length <code class=\"language-text\">0</code></td>\n<td>array length is <strong>not</strong> <code class=\"language-text\">0</code></td>\n</tr>\n</tbody>\n</table>\n<div class=\"filename\">array.js</div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// Empty array is different from sparse array</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// [], empty array</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">)</span> <span class=\"token comment\">// 0, empty array length 0</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">,</span><span class=\"token punctuation\">,</span><span class=\"token punctuation\">,</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ &lt;3 empty items> ], sparse array, empty element</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token punctuation\">,</span><span class=\"token punctuation\">,</span><span class=\"token punctuation\">,</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">)</span> <span class=\"token comment\">// 3, sparse array length 3</span></code></pre></div>\n<h2 id=\"removes-the-last-element-from-an-array-method-pop\" style=\"position:relative;\"><a href=\"#removes-the-last-element-from-an-array-method-pop\" aria-label=\"removes the last element from an array method pop permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Removes the last element from an array, method pop()</h2>\n<p>Syntax <code class=\"language-text\">array.pop()</code>.</p>\n<table>\n<thead>\n<tr>\n<th>Input</th>\n<th>Output</th>\n<th>Modify original array</th>\n<th>Time complexity (worst)</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>None</td>\n<td>popped element</td>\n<td>YES</td>\n<td>O(1)</td>\n</tr>\n</tbody>\n</table>\n<div class=\"filename\">array.js</div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// Removes the last element from an array, method pop</span>\n<span class=\"token keyword\">const</span> arr8 <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span><span class=\"token number\">4</span><span class=\"token punctuation\">]</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr8<span class=\"token punctuation\">.</span><span class=\"token function\">pop</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// 4, popped element</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr8<span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 2, 3 ], original array modified</span></code></pre></div>\n<h2 id=\"removes-the-first-element-from-an-array-method-shift\" style=\"position:relative;\"><a href=\"#removes-the-first-element-from-an-array-method-shift\" aria-label=\"removes the first element from an array method shift permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Removes the first element from an array, method shift()</h2>\n<p>Syntax <code class=\"language-text\">array.shift()</code>.</p>\n<table>\n<thead>\n<tr>\n<th>Input</th>\n<th>Output</th>\n<th>Modify original array</th>\n<th>Time complexity (worst)</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>None</td>\n<td>removed element</td>\n<td>YES</td>\n<td>O(1)</td>\n</tr>\n</tbody>\n</table>\n<div class=\"filename\">array.js</div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// Removes the first element from an array, method shift</span>\n<span class=\"token keyword\">const</span> arr9 <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'a'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'b'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'c'</span><span class=\"token punctuation\">]</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr9<span class=\"token punctuation\">.</span><span class=\"token function\">shift</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// a, 'shifted' element</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr9<span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 'b', 'c' ], original array modified</span></code></pre></div>\n<h2 id=\"adds-elements-to-the-end-of-an-array-method-push\" style=\"position:relative;\"><a href=\"#adds-elements-to-the-end-of-an-array-method-push\" aria-label=\"adds elements to the end of an array method push permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Adds element(s) to the end of an array, method push()</h2>\n<p>Syntax <code class=\"language-text\">array.push(element1, [element2, [element3 ...]])</code>.</p>\n<table>\n<thead>\n<tr>\n<th>Input</th>\n<th>Process</th>\n<th>Output</th>\n<th></th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>as many elements as wanted, separated by comma <code class=\"language-text\">,</code></td>\n<td>append new element(s) to the end of array</td>\n<td>last pushed element</td>\n<td></td>\n</tr>\n</tbody>\n</table>\n<table>\n<thead>\n<tr>\n<th>Modify original array</th>\n<th>Time complexity (worst)</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>YES</td>\n<td>O(1) for an element</td>\n</tr>\n</tbody>\n</table>\n<div class=\"filename\">array.js</div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// Adds element(s) to the end of an array, method push</span>\n<span class=\"token keyword\">const</span> arr10 <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'b'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr10<span class=\"token punctuation\">.</span><span class=\"token function\">push</span><span class=\"token punctuation\">(</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// 4, 'pushed' element</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr10<span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 1, 'b', 3, 4 ], original array modified</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr10<span class=\"token punctuation\">.</span><span class=\"token function\">push</span><span class=\"token punctuation\">(</span><span class=\"token number\">5</span><span class=\"token punctuation\">,</span><span class=\"token number\">6</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// 6, last 'pushed' element</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr10<span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 1, 'b', 3, 4, 5, 6 ], original array modified</span></code></pre></div>\n<h2 id=\"adds-elements-to-the-beginning-of-an-array-method-unshift\" style=\"position:relative;\"><a href=\"#adds-elements-to-the-beginning-of-an-array-method-unshift\" aria-label=\"adds elements to the beginning of an array method unshift permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Adds element(s) to the beginning of an array, method unshift()</h2>\n<p>Syntax <code class=\"language-text\">array.unshift(element1[, ...[, elementN]])</code>.</p>\n<table>\n<thead>\n<tr>\n<th>Input</th>\n<th>Process</th>\n<th>Output</th>\n<th></th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>as many elements as wanted, separated by comma <code class=\"language-text\">,</code></td>\n<td>append new element(s) to the beginning of array</td>\n<td>last pushed element</td>\n<td></td>\n</tr>\n</tbody>\n</table>\n<table>\n<thead>\n<tr>\n<th>Modify original array</th>\n<th>Time complexity (worst)</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>YES</td>\n<td>O(1) for an element</td>\n</tr>\n</tbody>\n</table>\n<div class=\"filename\">array.js</div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// Adds element(s) to the beginning of an array, method unshift</span>\n<span class=\"token keyword\">const</span> arr101 <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'b'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3</span><span class=\"token punctuation\">]</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr101<span class=\"token punctuation\">.</span><span class=\"token function\">unshift</span><span class=\"token punctuation\">(</span><span class=\"token number\">4</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// 4, 'unshifted' element</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr101<span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 4, 1, 'b', 3 ], original array modified</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr101<span class=\"token punctuation\">.</span><span class=\"token function\">unshift</span><span class=\"token punctuation\">(</span><span class=\"token number\">5</span><span class=\"token punctuation\">,</span><span class=\"token number\">6</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// 6, last 'unshifted' element</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr101<span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 5, 6, 4, 1, 'b', 3 ], original array modified</span></code></pre></div>\n<h2 id=\"adds-andor-removes-elements-from-an-array-method-splice\" style=\"position:relative;\"><a href=\"#adds-andor-removes-elements-from-an-array-method-splice\" aria-label=\"adds andor removes elements from an array method splice permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Adds and/or removes elements from an array, method splice()</h2>\n<p>Syntax <code class=\"language-text\">array.splice(start[, deleteCount[, item1[, item2[, ...]]]])</code>.</p>\n<p>Example <code class=\"language-text\">arr11 = [1,2,3,4,5]</code></p>\n<table>\n<thead>\n<tr>\n<th>Input</th>\n<th>What it does</th>\n<th>Output</th>\n<th>Original array</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td><code class=\"language-text\">arr11.splice(1)</code></td>\n<td>remove element from <code class=\"language-text\">index 1</code> to end</td>\n<td><code class=\"language-text\">[ 2, 3, 4, 5 ]</code></td>\n<td><code class=\"language-text\">[ 1 ]</code></td>\n</tr>\n<tr>\n<td><code class=\"language-text\">arr11.splice(1,2)</code></td>\n<td>remove <code class=\"language-text\">2</code> elements from <code class=\"language-text\">index 1</code></td>\n<td><code class=\"language-text\">[ 2, 3 ]</code></td>\n<td><code class=\"language-text\">[ 1, 4, 5 ]</code></td>\n</tr>\n<tr>\n<td><code class=\"language-text\">arr11.splice(1,2,'a','b','c')</code></td>\n<td>remove <code class=\"language-text\">2</code> elements from <code class=\"language-text\">index 1</code>, then <br> add element <code class=\"language-text\">a</code>, <code class=\"language-text\">b</code>, <code class=\"language-text\">c</code> at index 1</td>\n<td><code class=\"language-text\">[ 2, 3 ]</code></td>\n<td><code class=\"language-text\">[ 1, 'a', 'b', 'c', 4, 5 ]</code></td>\n</tr>\n</tbody>\n</table>\n<table>\n<thead>\n<tr>\n<th>Input</th>\n<th>Output</th>\n<th>Modify original array</th>\n<th>Time complexity (worst)</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>as example</td>\n<td>array of removed elements</td>\n<td>YES</td>\n<td>O(n + k) <code class=\"language-text\">n</code> is original array length <br> <code class=\"language-text\">k</code> is number of new elements to add</td>\n</tr>\n</tbody>\n</table>\n<div class=\"filename\">array.js</div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// Adds and/or removes elements from an array, method splice</span>\n<span class=\"token keyword\">const</span> arr11 <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span><span class=\"token number\">4</span><span class=\"token punctuation\">,</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr11<span class=\"token punctuation\">.</span><span class=\"token function\">splice</span><span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 2, 3, 4, 5 ], removed elements</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr11<span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 1 ], modified original array</span>\n\n<span class=\"token keyword\">const</span> arr12 <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span><span class=\"token number\">4</span><span class=\"token punctuation\">,</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr12<span class=\"token punctuation\">.</span><span class=\"token function\">splice</span><span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">2</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 2, 3 ], removed elements</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr12<span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 1, 4, 5 ], modified original array</span>\n\n<span class=\"token keyword\">const</span> arr13 <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span><span class=\"token number\">4</span><span class=\"token punctuation\">,</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr13<span class=\"token punctuation\">.</span><span class=\"token function\">splice</span><span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">2</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'a'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'b'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'c'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 2, 3 ], removed elements</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr13<span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 1, 'a', 'b', 'c', 4, 5 ], modified original array</span></code></pre></div>\n<h2 id=\"sorts-the-elements-of-an-array-method-sort\" style=\"position:relative;\"><a href=\"#sorts-the-elements-of-an-array-method-sort\" aria-label=\"sorts the elements of an array method sort permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Sorts the elements of an array, method sort()</h2>\n<p>Syntax <code class=\"language-text\">array.sort([compareFunction(a, b)])</code>. <code class=\"language-text\">a</code> and <code class=\"language-text\">b</code> are array elements.</p>\n<table>\n<thead>\n<tr>\n<th>Input</th>\n<th>Process</th>\n<th>Output</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>none</td>\n<td>Convert all elements to string and arrange to order of <a href=\"https://asecuritysite.com/coding/asc2\" target=\"_blank\" rel=\"nofollow\">UTF-16 table</a>. Default sort order is ascending</td>\n<td>sorted array</td>\n</tr>\n<tr>\n<td><code class=\"language-text\">compareFunction(a,b)</code></td>\n<td>- If function returns <code class=\"language-text\">less than 0</code>, sort <code class=\"language-text\">a</code> to an index lower than <code class=\"language-text\">b</code> (i.e. <code class=\"language-text\">a</code> comes first). <br> - If function returns <code class=\"language-text\">0</code>, leave <code class=\"language-text\">a</code> and <code class=\"language-text\">b</code> unchanged with respect to each other, but sorted with respect to all different elements. <br> - If function returns <code class=\"language-text\">greater than 0</code>, sort <code class=\"language-text\">b</code> to an index lower than <code class=\"language-text\">a</code> (i.e. <code class=\"language-text\">b</code> comes first).</td>\n<td>sorted array</td>\n</tr>\n</tbody>\n</table>\n<table>\n<thead>\n<tr>\n<th>Modify original array</th>\n<th>Time complexity (worst)</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>YES</td>\n<td>varies by browser, but safe to assume <code class=\"language-text\">O(nlogn)</code> <a href=\"https://stackoverflow.com/questions/234683/javascript-array-sort-implementation\" target=\"_blank\" rel=\"nofollow\">merge sort</a></td>\n</tr>\n</tbody>\n</table>\n<div class=\"filename\">array.js</div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// Sorts the elements of an array, method sort</span>\n<span class=\"token keyword\">const</span> arr14 <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">,</span><span class=\"token number\">3</span><span class=\"token punctuation\">,</span><span class=\"token number\">9</span><span class=\"token punctuation\">,</span><span class=\"token number\">7</span><span class=\"token punctuation\">]</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr14<span class=\"token punctuation\">.</span><span class=\"token function\">sort</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 3, 4, 7, 9 ], sorted array</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr14<span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 3, 4, 7, 9 ], original array modified</span>\n\n<span class=\"token keyword\">const</span> arr15 <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">[</span><span class=\"token string\">'c'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'b'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">4</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'a'</span><span class=\"token punctuation\">,</span> <span class=\"token number\">7</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">]</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr15<span class=\"token punctuation\">.</span><span class=\"token function\">sort</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">a<span class=\"token punctuation\">,</span>b</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> a<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span> <span class=\"token operator\">-</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n<span class=\"token comment\">// sort by the number</span>\n<span class=\"token comment\">// [ [ 'b', 4 ], [ 'c', 5 ], [ 'a', 7 ] ]</span>\n\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr15<span class=\"token punctuation\">.</span><span class=\"token function\">sort</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">a<span class=\"token punctuation\">,</span>b</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> a<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">charCodeAt</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">-</span> b<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">.</span><span class=\"token function\">charCodeAt</span><span class=\"token punctuation\">(</span><span class=\"token number\">0</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n<span class=\"token comment\">// sort by the character</span>\n<span class=\"token comment\">// [ [ 'a', 7 ], [ 'b', 4 ], [ 'c', 5 ] ]</span></code></pre></div>\n<h2 id=\"reverses-the-order-of-the-elements-of-an-array-method-reverse\" style=\"position:relative;\"><a href=\"#reverses-the-order-of-the-elements-of-an-array-method-reverse\" aria-label=\"reverses the order of the elements of an array method reverse permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reverses the order of the elements of an array, method reverse()</h2>\n<p>Syntax <code class=\"language-text\">array.reverse()</code>.</p>\n<table>\n<thead>\n<tr>\n<th>Input</th>\n<th>Process</th>\n<th>Output</th>\n<th></th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>None</td>\n<td>Reverse order of all element in array. Last element -> first element and so on.</td>\n<td>Reversed array</td>\n<td></td>\n</tr>\n</tbody>\n</table>\n<table>\n<thead>\n<tr>\n<th>Modify original array</th>\n<th>Time complexity (worst)</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>YES</td>\n<td><code class=\"language-text\">O(n)</code>, <code class=\"language-text\">n</code> is array length</td>\n</tr>\n</tbody>\n</table>\n<div class=\"filename\">array.js</div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// Reverses the order of the elements of an array, method reverse</span>\n<span class=\"token keyword\">const</span> arr16 <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'a'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'b'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'c'</span><span class=\"token punctuation\">]</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr16<span class=\"token punctuation\">.</span><span class=\"token function\">reverse</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 'c', 'b', 'a' ], reversed array</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr16<span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 'c', 'b', 'a' ], original array modified</span></code></pre></div>\n<h2 id=\"concatenate-array-with-other-arrays-andor-values-method-concat\" style=\"position:relative;\"><a href=\"#concatenate-array-with-other-arrays-andor-values-method-concat\" aria-label=\"concatenate array with other arrays andor values method concat permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Concatenate array with other array(s) and/or value(s), method concat()</h2>\n<p>Syntax <code class=\"language-text\">array.concat([value1[, value2[, ...[, valueN]]]])</code>.</p>\n<table>\n<thead>\n<tr>\n<th>Input</th>\n<th>Process</th>\n<th>Output</th>\n<th></th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Any kind of varible <br> As many as wanted</td>\n<td>Concatenate input(s) and original array</td>\n<td>One single array contains everything</td>\n<td></td>\n</tr>\n</tbody>\n</table>\n<table>\n<thead>\n<tr>\n<th>Modify original array</th>\n<th>Time complexity (worst)</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>NO</td>\n<td><code class=\"language-text\">O(n)</code>, <code class=\"language-text\">n</code> is number of input(s)</td>\n</tr>\n</tbody>\n</table>\n<div class=\"filename\">array.js</div>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// Concatenate array with other array(s) and/or value(s), method concat</span>\n<span class=\"token keyword\">const</span> arr17 <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token number\">1</span><span class=\"token punctuation\">,</span><span class=\"token number\">2</span><span class=\"token punctuation\">]</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr17<span class=\"token punctuation\">.</span><span class=\"token function\">concat</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">[</span><span class=\"token number\">4</span><span class=\"token punctuation\">,</span><span class=\"token number\">5</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token number\">6</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">,</span> <span class=\"token number\">7</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 1, 2, 4, 5, 6, 7 ]</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr17<span class=\"token punctuation\">.</span><span class=\"token function\">concat</span><span class=\"token punctuation\">(</span><span class=\"token string\">'a'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'b'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 1, 2, 'a', 'b' ]</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr17<span class=\"token punctuation\">.</span><span class=\"token function\">concat</span><span class=\"token punctuation\">(</span><span class=\"token string\">'c'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 1, 2, 'c' ]</span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>arr17<span class=\"token punctuation\">)</span> <span class=\"token comment\">// [ 1, 2 ], original array NOT modified</span></code></pre></div>\n<h2 id=\"summary\" style=\"position:relative;\"><a href=\"#summary\" aria-label=\"summary permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Summary</h2>\n<ul>\n<li>Array has a lot of built-in methods. They are not there to overwhelm you. They are there so you can use them when you need to. And you will.</li>\n<li>Remember what built-in methods can do while read/watch the lectures, syntax will become your muscle memory as you practice.</li>\n</ul>\n<p><a href=\"/javascript-built-in-data-structure-array-part-3-3/\">Continue to learn about array part 3/3</a>.</p>","timeToRead":8,"excerpt":"This tutorial is a part of the Learn everything about Javascript in one course. Javascript built-in data structure - Array, part 1/3 part…","frontmatter":{"title":"Javascript built-in data structure - Array, part 2/3","thumbnail":{"childImageSharp":{"fixed":{"base64":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAADFklEQVQ4y4VVSU8UQRTugaMHLlz8DaIwyBZkHWPEGeieAY0HJUSuxsTEhIuJB8ETRMJBXBITFK8evAoXY2I0hGCCgwONrDrAALOwKXRTz/dVd016gIROXup1V9VXX31vaU3zPKkZXUuZhvJ9bPmpGSM/M2v4tmbDGnznm+7DmrSpyz3HHgnkTmxEg/DztVMeF9iz38gFVGPaNPJc/wxbO9sQ2wjbKNswWydbgcPQ8OWCsp8yc8B8rn+DbdGOtxGtthFG5R+utBHPxdk6sDZjhnMISaobUyEvsy5s+rsYwUbLYzbG5LRu7c6HGfw65nu8TLN6Ks3ADGC8QACAg0Hbv6QvGEhszxlibyFM/B3gNq1J0E4vhqYi5mq26DKTYImoLpYmmgUfwIzDtDDeLCY/BcVaVMeh9g4z5bUJtsIsloddO3TihdZmTKfDeES8HQzQ5UApbTH4y6cNVF9XSmXlJfSoq0bsL4dpc1o/cFnezbJEbrkvQxCdc8vaiOlCrETEq/5Gqqry0+y3EJ0rukC9j+to/adO0c9B2nKksECCx/eulnkaktYFHFEMAWjHI/R6oJGqq/2UmNLpzu1KKq8oof4n9bQy2SK1TbI0B79bATihgqOhAlzAUfc0m68saL2V3r0IUAWDZFhD6Pi8r4Eqq0ro1s0KQpCg4/6yBPyeBfRceVisOgyZkYA9fHCJamv9tM4Mx0ev0e6CQX3ddVR0vlgGi6NuW38k4AcPYDYonWDImyyOJF29clEU+4vpzbMArf5ooWBTGdXU+KUEg70NYnfeAEsVlPueoGTTpgAVsMdpw+xsMIp9CYmdeee6c2PNNPaxScx8DYk9TiFcd3sOo57kfWe9KZhlyZMduDYS999S2MYVoRUSm4GRKoqZrBpKSHb3JIbpSKel3ZJRdcygPbgGTseVVNkhogDCN8y5YAPHSs9b2Bkzonx0lASAoes+pwZMNghHs6RidrRbndS+lKaFqAAkLfIMqYFoIgBKs6Pty9MPDe1IXzu9wZpGToNNx1pO7tqq86pfAMoJTJR5fwGpE34B/wEgbi1Y5n1kcAAAAABJRU5ErkJggg==","width":150,"height":150,"src":"/static/5e267e2ee412a23e797106ee564145a0/4148e/js.png","srcSet":"/static/5e267e2ee412a23e797106ee564145a0/4148e/js.png 1x,\n/static/5e267e2ee412a23e797106ee564145a0/de03e/js.png 1.5x"}}},"slug":"javascript-built-in-data-structure-array-part-2-3","date":"2020-03-11T00:00:00.000Z","categories":["Javascript","Frontend","Backend"],"tags":["javascript","fundamentals"],"template":"post"},"fields":{"slug":"/javascript-built-in-data-structure-array-part-2-3/","date":"2020-03-11T00:00:00.000Z"}}},"pageContext":{"slug":"/javascript-built-in-data-structure-array-part-2-3/"}},"staticQueryHashes":[]}