Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda