Javascript Arrays are an object that permits you to store multiple values into a collection for easy reference. For example, if you wanted to track a user’s position position over some time, you could insert the x and y values into an array. Arrays are more than just the example of x and y values though.
Let’s say we are building a calculator. Initially, we might think that we only need to variables. We need one for the first number before the operator is pressed and another one after. Congratulations on us being super static. Maybe we now want to be able to add more than just 2 values before calculating the sum. We should have used an array from the very beginning. If we would have used an array, we could have put all of those numbers in one single array, which is a 1000 times better than having to create a new variable for each number. It is a compliment in the world of programming if someone says your coding is dynamic. Arrays are in fact just that, dynamic.
Common Array Properties
constructor – tells us the function that created the Array object’s prototype
length – returns the total number of items in a array
Common Array Methods
As an object, arrays have a variety of methods. Since arrays are super dynamic, we have a lot of things we want to do with them. The creators of JavaScript gave us plenty of methods to use on our arrays.
concat() – this is the proper way to combine multiple arrays
indexOf() – finds item and returns it’s position
lastIndexOf() – finds item’s finale occurrence and returns it’s position
join() – joins all of the elements in an array into a string (values separated by a comma)
slice() – selects and returns part of an array
reverse() – reverses the order of the array
sort() – sorts elements as you would expect
splice() – add and/or removes elements from array
push() – adds items to the end of array, and returns position of the added item
pop() – removes and returns the last item of the array
shift() – removes and returns the first item of the array
unshift() – adds item to beginning of the array and returns its new length
Link/cite this page
If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.
Stewart, Suzy. "Arrays". After Hours Programming. Accessed on September 23, 2023. https://www.afterhoursprogramming.com/tutorial/javascript/arrays-js/.
Stewart, Suzy. Arrays. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/javascript/arrays-js/.
0 thoughts on “Arrays”
It seems that the push() method actually returns the new length of the array, not the position of the new item, as it says above. In the example a 4th item is pushed on a 3-item array. The position of the newest item is 3, but the method returns 4, which is the new length of the array.
It seems that the push() method actually returns the new length of the array, not the position of the new item, as it says above. In the example a 4th item is pushed on a 3-item array. The position of the newest item is 3, but the method returns 4, which is the new length of the array.
No actual line break in the first example (? am I missing something)