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?

share|improve this question
Works for me: jsfiddle.net/zzzHz – Rocket Hazmat 23 hours ago
Do you have any errors in your console? How is string being created? Is GetValue in scope? – Rocket Hazmat 23 hours ago
Do you want the output of the function GetValue() to be the type of string? In general, I don't think you should name a variable the same as a type. Consider changing the variable name 'string'. – Josh C. 23 hours ago
1  
Also, do you have a var declaration somewhere in the ...Do Something... section? Otherwise, the variable would be undefined if it is never declared and never assigned. – Josh C. 23 hours ago
@Josh, see "Additional Info" section I added to my OP for clarification. – eventide 21 hours ago
show 1 more comment
feedback

1 Answer

up vote 2 down vote accepted

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|improve this answer
1  
Face slap! That's what happens when you switch between PHP and JS too much! – eventide 21 hours ago
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.