Array Methods
👨💼 Arrays come with built-in methods to add, remove, and check elements.
Common array methods:
push(item)- Add to the endpop()- Remove from the endunshift(item)- Add to the beginningshift()- Remove from the beginninglength- Number of elements (property, not method)includes(item)- Check if item exists (returns boolean)
- Create an array
taskswith two items: "Learn JavaScript" and "Build project" - Use
pushto add "Deploy app" to the end - Use
unshiftto add "Plan features" to the beginning - Log the array length
- Check if "Learn JavaScript" is in the array using
includes - Use
popto remove the last item and log what was removed - Log the final array
💰 Example:
const numbers = [1, 2, 3]
numbers.push(4) // [1, 2, 3, 4]
const removed = numbers.pop() // removed = 4, numbers = [1, 2, 3]