0

I am trying to set an email address as a variable to use later. The email, as well as other profile information, is stored in a JSON file. Here is my JavaScript code:

var email = "missing";

$.getJSON("/api/v1/users/USERID/profile", function(profiledata) {
    var email = profiledata.primary_email;
});

console.log(email);

The result written to the console is "missing". I want the result to be the user's email.

However, if I were to do

$.getJSON("/api/v1/users/USERID/profile", function(profiledata) {
    console.log(profiledata.primary_email);
});

It will correctly output the email address value, but only to the console. How would I go about storing the email address as a variable for use later on?

1 Answers1

-2
var email = "missing";

$.getJSON("/api/v1/users/USERID/profile", function(profiledata) {
    email = profiledata.primary_email;
}).done(function() {
   console.log(email);
  });
Can Güven
  • 111
  • 5
  • 1
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Sep 26 '20 at 09:53