47 | const oldTags = []; 48 | for(let i = 0, j = headCountEl.previousElementSibling; i < headCount; i++, j = j.previousElementSibling){ > 49 | if (j.tagName.toLowerCase() === type) { | ^ 50 | oldTags.push(j); 51 | } 52 | }`
Advertisement
Answer
First Solution
use j?.tagName.toLowerCase()
instead of j.tagName.toLowerCase()
?.
is the Optional Chaining
concept in JavaScript
for more information you can check these links:
Second Solution
also if you don’t want to use ?.
, you must add some expressions in your condition
if (j && j.tagName && j.tagName.toLowerCase() === type) { oldTags.push(j); }