I was trying to learn some JS from this HTML5 canvas snake game guide.
But I’m stuck at the part with the push
:
for (var i = length; i >= 0; i--) {
snake.push({ x: i, y: 0 });
}
Can anyone explain this to me?
I was trying to learn some JS from this HTML5 canvas snake game guide.
But I’m stuck at the part with the push
:
for (var i = length; i >= 0; i--) {
snake.push({ x: i, y: 0 });
}
Can anyone explain this to me?
push is a method which pushes the data at the end of the existing array without having to know the index of your array. so if your data is not for a specific index and can be at the end in stack, just use the push method. the output would be-
old val of array1 : ['ab','bc', 'ca']
array1.push('cz');
new value of array1 : ['ab','bc', 'ca', 'cz']