1

I created code to get the URL parameter. I would just like to have a unique task for this function, not the implementation of the result, that the responsibilities will belong to other future functions.

I searched for various internet search combinations, like "get javascript return", "get function result", or something. But without success! In the end, it has to make the function have only one task in order to be manipulated by other functions.

function getURL(){

        var getURLs = window.location.search.substring(1).split('&');

        var getURLArray = {};

        for(var i=0; i<getURLs.length; i++) {

            var getURL = getURLs[i].split('=');

            getURLArray[getURL[0]] = getURL[1];
        }

        action = getURL[0]  ;
    }

The purpose of the function is to just get the value of the parameter, without having to use global variable or Storage API (localStorage or sessionStorage).

11
  • You'll want to return action at the end of that function. Commented Aug 22, 2019 at 2:17
  • why the loop if you only ever want getURL[0] - P.S. dont' declare a variable inside a for loop that you want to use outside of it - while this works with var, it's not good practice Commented Aug 22, 2019 at 2:19
  • It's like return = myResult; ? And how to share this result for another funcion without global variabe or Storage? Commented Aug 22, 2019 at 2:20
  • return = myResult; - this isn't VB6 ... it's like return xxxx where xxxx is the value you want to return, as a literal or a variable Commented Aug 22, 2019 at 2:21
  • 1
    function second can call function first ... you're just showing function definitions, never how you want to use them, so it's really hard to know what you want Commented Aug 22, 2019 at 2:27

1 Answer 1

1

Use the return keyword:

function getURL(){

        var getURLs = window.location.search.substring(1).split('&');

        var getURLArray = {};

        for(var i=0; i<getURLs.length; i++) {

            var getURL = getURLs[i].split('=');

            getURLArray[getURL[0]] = getURL[1];
        }

        action = getURL[0]  ;

        return action;
    }

To use the result in another place in your code you simple assign the result to a variable or just use it directly:

var myVar = getURL(); // You can use 'let' or 'const' to declare local variables.

// or

console.log(getURL());

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return

Sign up to request clarification or add additional context in comments.

5 Comments

Reason for downvote? If I missunderstood the question please let me know.
I didn't downgrade, but I have a question: Is using global the only way to get reusable function results in other functions? It is a sincere question!
@SérgioLima You can also use the result directly, like for example as an argument for another function call. I have edited my answer to include an example of using the returned value directly in console.log(). Is this what you were looking for?
@SérgioLima If you want to store the value in a local variable, inside another function for example, you can use the let or const keyword instead of var when you declare the variable. w3schools.com/jS/js_let.asp
I did it that way function action($value){ console.log($value); } function foo(){ // code return result; action(result); } So....yes! You did. But complete the answer with this last example, please, for a future people. Thanks!

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.