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

189
Visualizações
Return statement doesn't work as intended

So I was working with this line of code to convert an input image into an URL, however, the return doesn't seem to be working as I can't access the constant anywhere else in the document where I actually need it.

window.addEventListener('load', function() {
            document.querySelector('input[type="file"]').addEventListener('change', function() {
                if (this.files && this.files[0]) {
                    const imageURL = URL.createObjectURL(this.files[0]);
                    return imageURL;
                }
            });
          });       

I'm still new to JavaScript so for the life of me I can't find why it doesn't work, thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

That's because constant imageURL is scoped in if block, if you want it to be accessed in whole file declare it outside addEventListener() function. For e.g.

let imageURL;
window.addEventListener('load', function() {
            document.querySelector('input[type="file"]').addEventListener('change', function() {
                if (this.files && this.files[0]) {
                    imageURL = URL.createObjectURL(this.files[0]);
                    
                }
            });
          });

console.log(imageURL)
about 4 years ago · Juan Pablo Isaza 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