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

351
Views
saving variable in a function to outside the function in javascript

I have this code and I want to use the coinPrice variable outside the function, but I can't do it. Can anyone help me out ?

var burl = 'https://fapi.binance.com';
var query = '/fapi/v1/ticker/price?symbol=BNBUSDT';

var url = burl + query
var XMLHttpRequest = require('xhr2');
var xhr = new XMLHttpRequest();
var ourRequest = new XMLHttpRequest();

ourRequest.open('GET', url, true);

ourRequest.onload = function() {
  console.log(ourRequest.responseText);
  const coinPrice = ourRequest.responseText
  console.log(coinPrice)
}

ourRequest.send();

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

0

var coinPrice = ourRequest.responseText

Change "const" to "var"

or define it before with the "let" keyword

let coinPrice;
ourRequest.onload = function() {
  console.log(ourRequest.responseText);
  coinPrice = ourRequest.responseText
  console.log(coinPrice)
}
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!