简体   繁体   中英

Return JSON value from a function

I am able to pass a JSON string to my function but can't return a value back.

var json = {"First":"ABC", "Middle":"DEF", "Last":"GHI"};
allFunction6(json);
alert(first); //this does not work

function allFunction6(json) {
    var first = json.First;
    alert(first); //this alerts "ABC"
    return first;       
}

Does the variable not retain its value outside of the function, or am I missing something? Thanks.

Nope, the variable does not retain its value outside of the function, because that is where it is scoped to, the function.

You need to save the returned value.

var storedReturnValue = allFunction6(json);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM