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
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
Your code will execute in this window.
Let's explore some more tutorials or topics!
ericWS Dec. 11, 2013, 1:58 a.m.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.
JJChapman Oct. 18, 2013, 3:12 a.m.No actual line break in the first example (? am I missing something)