Assign value returned from a javascript function to a variable

13,143

The problem is this line:

console.log('targetFolder: ' . targetFolder); // console output: "undefined"

The . should be a +.

As written, your code is equivalent to doing this:

console.log('targetFolder'.targetFolder);

In other words, it evaluates to the targetFolder property of the string 'targetFolder' (coerced to a String instance) - which is undefined.

Share:
13,143
marky
Author by

marky

I work as a database migration/conversion programmer for a medical software company in rural central Minnesota. Tools used include SQL Server 2005, 2008 and 2012, VS2013, Tortoise SVN.

Updated on June 05, 2022

Comments

  • marky
    marky almost 2 years

    I have a function that returns a string, and I need to call the function several times and put the result in a variable.

    I have this:

    function GetValue() {
        ... Do Something ...
        console.log(string); // displays 'string'
        return string;
    }
    

    And I'm trying to assign the output of the function to a variable in another function. Currently I'm doing this:

    var theString = GetValue();
    console.log(theString); // displays 'undefined'
    

    What I don't understand is the console output in the function is displaying the value, but it's not after assigning the variable to the output of the function.

    Obviously this isn't the way to assign the output of a function to a variable. So how do I do that?

    [ADDITIONAL INFO]

    Apparently my attempt at being brief in my sample code only served to cause confusion.

    Here is the full function that I need to call several times from elsewhere in the javascript file:

    /*
    * Build URL link
    */
    function BuildUrlTargetFolder() {
        // Get the current url and remove the 'zipCodes/branchAdmin' part of it for the branch url link
        var urlArray = window.location.href.split('/'),
        targetLength = urlArray.length - 3,
        i,
        targetFolder = '';
    
        for (i = 0; i < targetLength; i++) {
            // Only add a slash after the 'http:' element of the array
            if (i === 0) {
                targetFolder += urlArray[i];
            } else {
                targetFolder += '/' + urlArray[i];
            }
        }
    
        console.log('targetFolder: ' + targetFolder); // console output is the current url minus two "levels"
    
        return targetFolder;
    }
    

    Here's one of the places that the function needs to be used:

    var targetFolder = BuildUrlTargetFolder();
    
    console.log('targetFolder: ' . targetFolder); // console output: "undefined"
    
    // If the url has a value, set the href attribute for the branch link otherwise hide the url
    if (data['url'] !== '') {
        $('a#branchLink').attr('href', targetFolder + '/index.php/coverage-area/' + data['url']).show();
    } else {
        $('a#branchLink').attr('href', '#').hide();
    }
    

    So, having said that, how do I get the string from the function assigned to the calling code's variable?