When working with Arrays, isn't push and length identical ? If so why do I see length property used most frequently ? I personally am growing use to using push, was a little easier for me to remember. In my discoveries I find the length property used instead.

What is the following mean;

one[one.length-1] ?

I know one[one.length] will add to the end of the array, what is -1 doing to the array ? Can you do +2, -2, +4 etc to the length property ?

push adds a new item to the array. length just returns the number of items in the array. length - 1 is the last item in the array, since an array is zero-based.

Thanks :)

Length takes one away from the array :)

Length is a property and as mentioned above provides you with the number of items in the array. Do not be confused about length - 1.

Say that the length is 3. That means there are three items. For example arr[0], arr[1], and arr[2].

You can access the last member by using length - 1 without having knowledge of how many items there actually are.

So... arr[arr.length-1] is the same as arr[2] is this example.

I tried length-2, -3, -4, I assume you can only use length-1 ?

Should work fine if your array is large enough. Can you the code you tried?

This code will write "fri".

var today = ["mon","tues","wed","thur"];
var solid = today[today.length]="fri"
document.write(today[4])

This code won't write "thur" as "thur" is taken out of the array with length-1 and "thur" is replaced with "fri" as index=3.

var today = ["mon","tues","wed","thur"];
var solid = today[today.length-1]="fri"
document.write(today[3])

Ah, yes. By assigning "fri" to array[length] you are implicitly adding a new value to your array. This is fiddling with Javascript flaws imo.

If you write array#3 you will get undefinied, therefore length-1 must be removing the last item from the array. Therefore in the above example; "thur" gets replaced with "fri" and the array stays at four items in the array, instead of five.

The way I understand it length is just adding to the array, it's not returning the total number of items in the array, which is the same as push().

No, it doesn't seem like you understand length. Keep in mind its a property not a method. Think of it as a noun instead of a verb. Length has a value. You cannot use it to do something.

So to reiterate, length holds a value which represents the total number of items in the array. It holds a number.

If length holds the total number of items in the array, as in my example that would be four, then how come when I use length-1 it removes the last item from the array, but at the same time adds an item to the array ? Shouldn't it just add the extra item to the array and that's it ?

I use length-1 it removes the last item from the array, but at the same time adds an item to the array

You are overwriting the contents of the last item in the array when using length-1.

When you use length or length+N Javascript assumes you want to add items to the existing array and does the push for you implicitly.

That is what I thought, now you confirmed it :)

I tried length+2, and length -2, those didn't work without a undefined error, I assume only length-1 is usable ?

I assume only length-1 is usable

No, take a step back and understand what length - 1 means. Think of length as a variable. it holds a value. So lets say this value is 4. When you reference [length - 1], if legnth is 4, it the result is 3 because you subtracted from the current legth. This itself does nothing to your array because all you did was some math. However, if you had this example... arr[arr.length - 1], and length is 4, since you subtracted 1, the value in [] is 3, so you are referencing arr[3]. If you had arr[arr.length - 2] and length is 4, now you subtracted 2, so the result is 2. Therefore you are referring to arr[2]. If you do arr[arr.legnth + 2], then it would have resulted in arr[6].

What pritaes mentioned is that if you reference an item in the array that doesnt exist, javascript will push for you and create that item in the array. Think of length as a variable. Its purpose is to hold a value. It doesnt perform any action on your array.

I understand.

The only thing arr[arr.length+2]="arr1","arr2"; does not add two new items to the array. Using the substraction symbol works, not the addition.

var today = ["mon", "tue", "wed", "thu"];
var solid = today[today.length + 2] = "sun"
document.write(today[4]);
document.write(today[5]);
document.write(today[6]);

The array values 4 and 5 are both undefined. You told to add more items, but only set the last one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.