JavaScript: why does not work getting the last element of the array?

Hello!
Why not work getting the last element of the array? This is the brief record?

document.querySelectorAll('head > link[rel="stylesheet"]')[this.length-1];


Thank you!
June 8th 19 at 16:38
1 answer
June 8th 19 at 16:40
Solution
What do you expect from this.length-1 ? Depending on the calling context you have in this can be anything. If there is a window (window.length-1) == -1, and the element with index -1 in the array can not be.

let stylesheets = document.querySelectorAll('head > link[rel="stylesheet"]')
let last = Array.from(stylesheets).pop()
// or
let last = [...stylesheets].pop()

Find more questions by tags JavaScript