Course Content
Introduction to JavaScript
So, you can use an array for storing and retrieving data. The retrieving process by []
calls indexing.
But there are different methods for working with an array.
Add element
There are different ways to add an element to an array.
Push
The push()
method adds a new value to the end of the array:
Unshift
The unshift()
method works like the push()
method, but it inserts the value at the beginning of the array.
Indexing
You can append a new value by indexing:
You can use indexing to assign a value to a specified index, reassign a previous value, etc:
To create a new element in the array without mistakes, you need to use the push(value)
method or arr[arr.length] = value
expression:
Delete element
Sometimes you may need to delete array elements. This can be done in different ways. Now I will describe some of them.
Pop
The pop()
method deletes the last element in an array, and you can save it to another variable:
Shift
The shift()
method works like the pop()
method, but cuts the first element from an array:
How to add an element to the end of the array?
Select a few correct answers
How to remove the last element in the array?
Select the correct answer
Section 2.
Chapter 7