JavaScript · JS Tutorial · Lesson 20
JS Array Methods
pop, shift, splice, slice, concat, join and includes.
Arrays come with many built-in methods.
Example
<p id="demo"></p>
<script>
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let out = "";
out += fruits.join(" * ") + "<br>";
fruits.pop(); // removes Mango
fruits.shift(); // removes Banana
fruits.unshift("Lemon"); // adds at the start
fruits.splice(1, 0, "Kiwi", "Plum"); // inserts at index 1
out += fruits + "<br>";
out += fruits.slice(1, 3) + "<br>";
out += fruits.concat(["Cherry"]) + "<br>";
out += fruits.includes("Kiwi") + ", index of Apple: " + fruits.indexOf("Apple");
document.getElementById("demo").innerHTML = out;
</script>
| Method | Does |
|---|---|
| pop() / push() | Remove / add at the end |
| shift() / unshift() | Remove / add at the start |
| splice(i, n, ...items) | Remove and/or insert items (changes the array) |
| slice(start, end) | Copy a part into a new array |
| concat() | Merge arrays |
| join(sep) | Join elements into a string |
Keep reading “JS Array Methods”
Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.