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

95
Views
Javascript Function Call have different behaviour compared to run by console in web browser

i have this code:

function foo(){
    var x = $.getJSON('./someFileName.txt',function(data){});
    var y = x.responseText;
    console.log(y);
}

when i called it via browser console (firefox), it give me undefined for the console log result.

But, when i just copy, paste and, run the foo() function body line by line in my browser console, it gives me a result which is a string.

Can anyone enlighten me why this behaviour happens? I suppose it has something to do with the jquery call but i am not sure.

Thanks!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to place the second and third lines of your function inside the callback like this:

function foo(){
    $.getJSON('./someFileName.txt',function(data){
    var y = data.responseText;
    console.log(y);
    }); 
}
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!