Converting Arrays to Strings
The JavaScript method toString()
converts an array to a string of (comma separated) array values.
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>toString()</h2> <p>The toString() method returns an array as a comma separated string:</p> <p id="demo"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits.toString(); </script> </body> </html>
Result:
JavaScript Array Methods
toString()
The toString() method returns an array as a comma separated string:
Banana,Orange,Apple,Mango
The join()
method also joins all array elements into a string.
It behaves just like toString()
, but in addition you can specify the separator:
Example
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array Methods</h2>
<h2>join()</h2>
<p>The join() method joins array elements into a string.</p><p>It this example we have used ” * ” as a separator between the elements:</p>
<p id=”demo”></p>
<script>
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
document.getElementById(“demo”).innerHTML = fruits.join(” * “);
</script>
</body>
</html>
Result:
JavaScript Array Methods
join()
The join() method joins array elements into a string.
It this example we have used ” * ” as a separator between the elements:
Banana * Orange * Apple * Mango
Popping and Pushing
When you work with arrays, it is easy to remove elements and add new elements.
This is what popping and pushing is:
Popping items out of an array, or pushing items into an array.
JavaScript Array pop()
The pop()
method removes the last element from an array:
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>pop()</h2> <p>The pop() method removes the last element from an array.</p> <p id="demo1"></p> <p id="demo2"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = fruits; fruits.pop(); document.getElementById("demo2").innerHTML = fruits; </script> </body> </html>
Result:
JavaScript Array Methods
pop()
The pop() method removes the last element from an array.
Banana,Orange,Apple,Mango
Banana,Orange,Apple
The pop()
method returns the value that was “popped out”:
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>pop()</h2> <p>The pop() method returns the value that was "popped out":</p> <p id="demo1"></p> <p id="demo2"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = fruits.pop(); document.getElementById("demo2").innerHTML = fruits; </script> </body> </html>
Result:
JavaScript Array Methods
pop()
The pop() method returns the value that was “popped out”:
Mango
Banana,Orange,Apple
JavaScript Array push()
The push()
method adds a new element to an array (at the end):
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>push()</h2> <p>The push() method appends a new element to an array:</p> <p id="demo1"></p> <p id="demo2"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = fruits; fruits.push("Kiwi"); document.getElementById("demo2").innerHTML = fruits; </script> </body> </html>
Result:
JavaScript Array Methods
push()
The push() method appends a new element to an array:
Banana,Orange,Apple,Mango
Banana,Orange,Apple,Mango,Kiwi
The push()
method returns the new array length:
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>push()</h2> <p>The push() method returns the new array length:</p> <p id="demo1"></p> <p id="demo2"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = fruits.push("Kiwi"); document.getElementById("demo2").innerHTML = fruits; </script> </body> </html>
Result:
JavaScript Array Methods
push()
The push() method returns the new array length:
5
Banana,Orange,Apple,Mango,Kiwi
Shifting Elements
Shifting is equivalent to popping, but working on the first element instead of the last.
JavaScript Array shift()
The shift()
method removes the first array element and “shifts” all other elements to a lower index.
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>shift()</h2> <p>The shift() method removes the first element of an array (and "shifts" the other elements to the left):</p> <p id="demo1"></p> <p id="demo2"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = fruits; fruits.shift(); document.getElementById("demo2").innerHTML = fruits; </script> </body> </html>
Result:
JavaScript Array Methods
shift()
The shift() method removes the first element of an array (and “shifts” the other elements to the left):
Banana,Orange,Apple,Mango
Orange,Apple,Mango
The shift()
method returns the value that was “shifted out”:
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>shift()</h2> <p>The shift() method returns the element that was shifted out.</p> <p id="demo1"></p> <p id="demo2"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = fruits.shift(); document.getElementById("demo2").innerHTML = fruits; </script> </body> </html>
Result:
JavaScript Array Methods
shift()
The shift() method returns the element that was shifted out.
Banana
Orange,Apple,Mango
JavaScript Array unshift()
The unshift()
method adds a new element to an array (at the beginning), and “unshifts” older elements:
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>unshift()</h2> <p>The unshift() method adds new elements to the beginning of an array:</p> <p id="demo1"></p> <p id="demo2"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = fruits; fruits.unshift("Lemon"); document.getElementById("demo2").innerHTML = fruits; </script> </body> </html>
Result:
JavaScript Array Methods
unshift()
The unshift() method adds new elements to the beginning of an array:
Banana,Orange,Apple,Mango
Lemon,Banana,Orange,Apple,Mango
The unshift()
method returns the new array length.
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>unshift()</h2> <p>The unshift() method returns the length of the new array:</p> <p id="demo1"></p> <p id="demo2"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = fruits.unshift("Lemon"); document.getElementById("demo2").innerHTML = fruits; </script> </body> </html>
Result:
JavaScript Array Methods
unshift()
The unshift() method returns the length of the new array:
5
Lemon,Banana,Orange,Apple,Mango
Changing Elements
Array elements are accessed using their index number:
Array indexes start with 0:
[0] is the first array element
[1] is the second
[2] is the third …
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <p>Array elements are accessed using their index number:</p> <p id="demo1"></p> <p id="demo2"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = fruits; fruits[0] = "Kiwi"; document.getElementById("demo2").innerHTML = fruits; </script> </body> </html>
Result:
JavaScript Array Methods
Array elements are accessed using their index number:
Banana,Orange,Apple,Mango
Kiwi,Orange,Apple,Mango
JavaScript Array length
The length
property provides an easy way to append a new element to an array:
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <p>The length property provides an easy way to append new elements to an array without using the push() method:</p> <p id="demo1"></p> <p id="demo2"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; }document.getElementById("demo1").innerHTML = fruits; fruits[fruits.length] = "Kiwi"; document.getElementById("demo2").innerHTML = fruits; </script> </body> </html>
Result:
JavaScript Array Methods
The length property provides an easy way to append new elements to an array without using the push() method:
Banana,Orange,Apple,Mango
Banana,Orange,Apple,Mango,Kiwi
JavaScript Array delete()
Warning !
Array elements can be deleted using the JavaScript operator delete
.
Using delete
leaves undefined
holes in the array.
Use pop() or shift() instead.
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <p>Deleting elements leaves undefined holes in an array:</p> <p id="demo1"></p> <p id="demo2"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = "The first fruit is: " + fruits[0]; delete fruits[0]; document.getElementById("demo2").innerHTML = "The first fruit is: " + fruits[0]; </script> </body> </html>
Result:
JavaScript Array Methods
Deleting elements leaves undefined holes in an array:
The first fruit is: Banana
The first fruit is: undefined
Merging (Concatenating) Arrays
The concat()
method creates a new array by merging (concatenating) existing arrays:
Example (Merging Two Arrays)
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>concat()</h2> <p>The concat() method merges (concatenates) arrays:</p> <p id="demo"></p> <script> const myGirls = ["Cecilie", "Lone"]; const myBoys = ["Emil", "Tobias", "Linus"]; const myChildren = myGirls.concat(myBoys); document.getElementById("demo").innerHTML = myChildren; </script> </body> </html>
Result:
JavaScript Array Methods
concat()
The concat() method merges (concatenates) arrays:
Cecilie,Lone,Emil,Tobias,Linus
The concat()
method does not change the existing arrays. It always returns a new array.
The concat()
method can take any number of array arguments:
Example (Merging Three Arrays)
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>concat()</h2> <p>The concat() method merges (concatenates) arrays:</p> <p id="demo"></p> <script> const array1 = ["Cecilie", "Lone"]; const array2 = ["Emil", "Tobias", "Linus"]; const array3 = ["Robin", "Morgan"]; const myChildren = array1.concat(array2, array3); document.getElementById("demo").innerHTML = myChildren; </script> </body> </html>
Result:
JavaScript Array Methods
concat()
The concat() method merges (concatenates) arrays:
Cecilie,Lone,Emil,Tobias,Linus,Robin,Morgan
The concat()
method can also take strings as arguments:
Example (Merging an Array with Values)
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>concat()</h2> <p>The concat() method can merge string values to arrays:</p> <p id="demo"></p> <script> const myArray = ["Emil", "Tobias", "Linus"]; const myChildren = myArray.concat("Peter"); document.getElementById("demo").innerHTML = myChildren; </script> </body> </html>
Result:
JavaScript Array Methods
concat()
The concat() method can merge string values to arrays:
Emil,Tobias,Linus,Peter
Splicing and Slicing Arrays
The splice()
method adds new items to an array.
The slice()
method slices out a piece of an array.
JavaScript Array splice()
The splice()
method can be used to add new items to an array:
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>splice()</h2> <p>The splice() method adds new elements to an array:</p> <p id="demo1"></p> <p id="demo2"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = fruits; fruits.splice(2, 0, "Lemon", "Kiwi"); document.getElementById("demo2").innerHTML = fruits; </script> </body> </html>
Result:
JavaScript Array Methods
splice()
The splice() method adds new elements to an array:
Banana,Orange,Apple,Mango
Banana,Orange,Lemon,Kiwi,Apple,Mango
The first parameter (2) defines the position where new elements should be added (spliced in).
The second parameter (0) defines how many elements should be removed.
The rest of the parameters (“Lemon” , “Kiwi”) define the new elements to be added.
The splice()
method returns an array with the deleted items:
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>splice()</h2> <p>The splice() method adds new elements to an array, and returns an array with the deleted elements (if any):</p> <p id="demo1"></p> <p id="demo2"></p> <p id="demo3"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = "Original Array:<br> " + fruits; let removed = fruits.splice(2, 2, "Lemon", "Kiwi"); document.getElementById("demo2").innerHTML = "New Array:<br>" + fruits; document.getElementById("demo3").innerHTML = "Removed Items:<br> " + removed; </script> </body> </html>
Result:
JavaScript Array Methods
splice()
The splice() method adds new elements to an array, and returns an array with the deleted elements (if any):
Original Array:
Banana,Orange,Apple,Mango
New Array:
Banana,Orange,Lemon,Kiwi
Removed Items:
Apple,Mango
Using splice() to Remove Elements
With clever parameter setting, you can use splice()
to remove elements without leaving “holes” in the array:
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>splice()</h2> <p>The splice() methods can be used to remove array elements:</p> <p id="demo1"></p> <p id="demo2"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo1").innerHTML = fruits; fruits.splice(0, 1); document.getElementById("demo2").innerHTML = fruits; </script> </body> </html>
Result:
JavaScript Array Methods
splice()
The splice() methods can be used to remove array elements:
Banana,Orange,Apple,Mango
Orange,Apple,Mango
The first parameter (0) defines the position where new elements should be added (spliced in).
The second parameter (1) defines how many elements should be removed.
The rest of the parameters are omitted. No new elements will be added.
JavaScript Array slice()
The slice()
method slices out a piece of an array into a new array.
This example slices out a part of an array starting from array element 1 (“Orange”):
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>slice()</h2> <p>This example slices out a part of an array starting from array element 1 ("Orange"):</p> <p id="demo"></p> <script> const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; const citrus = fruits.slice(1); document.getElementById("demo").innerHTML = fruits + "<br><br> + citrus; </script> </body> </html>
Result:
JavaScript Array Methods
slice()
This example slices out a part of an array starting from array element 1 (“Orange”):
Banana,Orange,Lemon,Apple,Mango
Orange,Lemon,Apple,Mango
Note
The slice()
method creates a new array.
The slice()
method does not remove any elements from the source array.
This example slices out a part of an array starting from array element 3 (“Apple”):
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>slice()</h2> <p>This example slices out a part of an array starting from array element 3 ("Apple")</p> <p id="demo"></p> <script> const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; const citrus = fruits.slice(3); document.getElementById("demo").innerHTML = fruits + "<br><br>" + citrus; </script> </body> </html>
Result:
JavaScript Array Methods
slice()
This example slices out a part of an array starting from array element 3 (“Apple”)
Banana,Orange,Lemon,Apple,Mango
Apple,Mango
The slice()
method can take two arguments like slice(1, 3)
.
The method then selects elements from the start argument, and up to (but not including) the end argument.
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>slice()</h2> <p>When the slice() method is given two arguments, it selects array elements from the start argument, and up to (but not included) the end argument:</p> <p id="demo"></p> <script> const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; const citrus = fruits.slice(1,3); document.getElementById("demo").innerHTML = fruits + "<br><br>" + citrus; </script> </body> </html>
Result:
JavaScript Array Methods
slice()
When the slice() method is given two arguments, it selects array elements from the start argument, and up to (but not included) the end argument:
Banana,Orange,Lemon,Apple,Mango
Orange,Lemon
If the end argument is omitted, like in the first examples, the slice()
method slices out the rest of the array.
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>slice()</h2> <p>This example slices out a part of an array starting from array element 2 ("Lemon"):</p> <p id="demo"></p> <script> const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"]; const citrus = fruits.slice(2); document.getElementById("demo").innerHTML = fruits + "<br><br>" + citrus; </script> </body> </html>
Result:
JavaScript Array Methods
slice()
This example slices out a part of an array starting from array element 2 (“Lemon”):
Banana,Orange,Lemon,Apple,Mango
Lemon,Apple,Mango
Automatic toString()
JavaScript automatically converts an array to a comma separated string when a primitive value is expected.
This is always the case when you try to output an array.
These two examples will produce the same result:
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <h2>toString()</h2> <p>The toString() method returns an array as a comma separated string:</p> <p id="demo"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits.toString(); </script> </body> </html>
Result:
JavaScript Array Methods
toString()
The toString() method returns an array as a comma separated string:
Banana,Orange,Apple,Mango
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Array Methods</h2> <p>JavaScript automatically converts an array to a comma separated string when a simple value is expected:</p> <p id="demo"></p> <script> const fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits; </script> </body> </html>
Result:
JavaScript Array Methods
JavaScript automatically converts an array to a comma separated string when a simple value is expected:
Banana,Orange,Apple,Mango