Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!