Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
Why for...of stops?

I have a piece of HTML:

<a name="list">the list</a>
<ul>
  <li><a href="http://google.com">http://google.com</a></li>
  <li><a href="/tutorial">/tutorial.html</a></li>
  <li><a href="local/path">local/path</a></li>
  <li><a href="ftp://ftp.com/my.zip">ftp://ftp.com/my.zip</a></li>
  <li><a href="http://nodejs.org">http://nodejs.org</a></li>
  <li><a href="http://internal.com/test">http://internal.com/test</a></li>
</ul>

and a piece of JS:

let links = document.querySelectorAll('a');
for (let link of links) {
  let href = link.getAttribute('href');
  alert(href.includes("://"))

The question is why the JS above doesn't work (it should display true/false), but works if I add an if-condition:

let links = document.querySelectorAll('a');
for (let link of links) {
  let href = link.getAttribute('href');
  if (!href) continue;
  alert(href.includes("://"))
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

It doesn't work when you didn't add if statement because there is a case href returns null for the link list you are trying. For your information, I share the console.log message when it doesn't work.

VM260:4 Uncaught TypeError: Cannot read properties of null (reading 'includes')
at <anonymous>:4:14

If you add if statement, then the includes is not executed in the code snippet. Hope that's clear to you.

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda