1

I have the following:

function getPk(entity) {
    var store = window.localStorage;
    switch (entity) {
        case "City":
            if (store.getItem('AccountID')) {
                // Need to return both of the below pieces of information
                return store.getItem('AccountID') + "04" + "000";
                return "CityTable";

            } else {
                paramOnFailure("Please reselect"); 
                return false;
            }
            break;

The problem is I need to be able to call this function and return two strings. Here I show two return statements but I know I cannot do that.

Is there a clean way I can return two strings to the function that calls my getPk(entity) function?

If possible can you give an example also of how I can read what is returned.

3 Answers 3

3

Return them as either an array, or within an object.

return [store.getItem('AccountID') + "04" + "000", "CityTable"];

or

return { accountID: store.getItem('AccountID') + "04" + "000", table: "CityTable" };
Sign up to request clarification or add additional context in comments.

1 Comment

I'm sorry but then how do I read the array? Can you give an example of the calling function and what comes back. Thanks in advance.
3

You can only return a single value from a function in JavaScript, but that value can be a structure that contains multiple values internally, such as an array:

return [store.getItem('AccountID') + "04" + "000", "CityTable"];

Just make sure that the functions which call this know about the conventions you're using for your return value.

2 Comments

But can you show me how the calling function would read this. I'm sorry but my knowledge of javascript is very basic.
var x = getPk(entity);, then refer to x[0] and x[1] to refer to the two values you returned.
0

In javascript and like almost every languages, It is impossible to return more than one value.

However, you can return a custom object containing all the values you want to return.

See the following link to find how to create an object. Create an object with properties,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.