I'm scraping some data from a webpage using JQuery, but when I try to set the value to a local variable, it becomes undefined. I've searched everywhere on the scope of $.get() method as well as other instances where a variable is undefined when returned but not when printed to the console and I've hit a dead end. Is there something I'm missing? Why can't I point to the variable inside $.get()?
function scrape(url) {
let imageUrl = "";
$.get(url, function(response) {
imageUrl = response.toString().split("<meta property=\"og:image\" content=")[1].split("\"")[1];
console.log(imageUrl); // THIS PART WORKS AND PRINTS DESIRED URL
});
console.log(imageUrl); // this prints nothing...
return imageUrl; // returns undefined...
}
scrape("https://www.instagram.com/p/BfKjQxcgv-E/");