Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push" rel="noreferrer"><code>push()</code></a> function to append to an array:</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>// initialize array var arr = [ "Hi", "Hello", "Bonjour" ]; // append new value to the array arr.push("Hola"); console.log(arr);</code></pre> </div> </div> </p> <p>Will print</p> <pre><code>["Hi", "Hello", "Bonjour", "Hola"] </code></pre> <hr> <p>You can use the <code>push()</code> function to append more than one value to an array in a single call:</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>// initialize array var arr = [ "Hi", "Hello", "Bonjour", "Hola" ]; // append multiple values to the array arr.push("Salut", "Hey"); // display all values for (var i = 0; i &lt; arr.length; i++) { console.log(arr[i]); }</code></pre> </div> </div> </p> <p>Will print</p> <pre><code>Hi Hello Bonjour Hola Salut Hey </code></pre> <hr> <p><strong>Update</strong></p> <p>If you want to add the items of one array to another array, you can use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat" rel="noreferrer"><code>firstArray.concat(secondArray)</code></a>:</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>var arr = [ "apple", "banana", "cherry" ]; arr = arr.concat([ "dragonfruit", "elderberry", "fig" ]); console.log(arr);</code></pre> </div> </div> </p> <p>Will print</p> <pre><code>["apple", "banana", "cherry", "dragonfruit", "elderberry", "fig"] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload