I usually advise against using globals. It is better to have the variable as a return value of your function or have a callback argument which you can call with your value as soon as you are done (for example, in your getJSON callback function):
function PrepareJsonFormat(callback) {
var myData = {}; // all data will be stored here
jQuery.getJSON(
"/abc/xyz", { varA: jQuery("#qwerty").val() },
function (data) {
if (data != null && data != "") {
$.each(data, function (i, val) {
// put the data in your variable
myData[i] = val;
});
// when we are done, call the callback with myData
callback(myData);
}
});
}
Use like this:
PrepareJsonFormat( function(returnedData) {
// use returnedData (myData) here
});
The advantage over using globals is that if something calls your function twice, you don't overwrite the global. Also, any other javascript will have access to your global variables, so if somebody else uses the same name (and trust me, they will :) ), your data might get overwritten before you can use it. Also, other scripts might have already declared a global variable with that name (bad practice, but people do it), in this case you might break other scripts on your page.