Reference to the previous element in the array – in Javascript

πŸ‘:0

After you push a new element onto an array, the element before it is the 2nd to last element.

Unlike some other languages (e.g. Python) there’s no shorthand syntax to index from the end of an array. So you have to calculate the index by subtracting from array.length.

let deg = Math.atan2(message.x - arrX[arrX.length-2], message.y - arrY[arrY.length-2]) * 180 / Math.PI;

Leave a comment