Answer the question
In order to leave comments, you need to log in
JavaScript: Why doesn't getting the last element of an array work?
Hello!
Why doesn't getting the last element of an array work? Is this the shortest entry?
document.querySelectorAll('head > link[rel="stylesheet"]')[this.length-1];
Answer the question
In order to leave comments, you need to log in
What do you expect from this.length-1
? Depending on the context of the call, you can have anything in this. If there is window, (window.length-1) == -1, and there cannot be an element with index -1 in the array.
let stylesheets = document.querySelectorAll('head > link[rel="stylesheet"]')
let last = Array.from(stylesheets).pop()
// или
let last = [...stylesheets].pop()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question