1

I have a function that replaces characters from a string

function ratko(a) {
    var k = a.toString();
    var z = k.replace(/\,/g, '], [');
    var s = z.replace(/\./g, ', ');
    var final = "[[" + s + "]]";
    alert(final);
}

What I need is to get the value of final outside the function like this:

var outsideValue = final;

EDIT!!! --

function ratko() gets it's value from ajax

success: function (data) {
if (data.success) {
alert("Note: This month has " + data.holidays.length + " holidays.");
praznici = data.holidays;
ratko(praznici);
}
else {
alert(data.ErrorMessage);
}

3
  • return final;, then var ousideValue = ratko(a);? Commented Aug 27, 2012 at 8:54
  • "gets it's value from ajax". Something tells me it is yet another case of ignoring A in AJAX. Commented Aug 27, 2012 at 9:23
  • I've made jsFiddle - jsfiddle.net/cj4Er You can see the problem. I need "holiDays" to get the value of "Globalna" Commented Aug 27, 2012 at 9:36

4 Answers 4

1

Possibility 1:

function ratko (a) {
   ...
   return final;
}

var outsideValue = ratko (...);

Possibility 2:

var final;
function ratko (a) {
   // no var final declaration here
   ...
}
...
ratko (...);
// now final has the value assigned to it in the function

You can access variables declared in an outer scope in an inner scope, which is what you do in Possibility 2.

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

1 Comment

Thank you JohnB but that does not solver my issue. I still get "undefined".
0

One option would be to use a global variable, just declare it outside of the function.

var myvar = 1;
function myFunction()
   alert(myvar); // 1
}

You can read more on javascript variables here.

Comments

0

You declare it outside the function scope:

  var finalVar;

function ratko(a) {
var k = a.toString();
var z = k.replace(/\,/g, '], [');
var s = z.replace(/./g, ', ');
 finalVar= "[[" + s + "]]";
alert(finalVar);
}

var outsideValue = finalVar;

Beware final is a reserved keyword in Javascript. I changed its name. Besides that, keep in mind that Javascript is always parsed from top to bottom. So using a variable before declaring it will definitely give you an undefined.

3 Comments

What I did was this: var final; function ratko(a) { var k = a.toString(); var z = k.replace(/\,/g, '], ['); var s = z.replace(/\./g, ', '); final = "[[" + s + "]]"; } var outsideValue = final; document.write(outsideValue); And it gives me "undefined";
Where do you call the function?
I've made jsFiddle - jsfiddle.net/cj4Er You can see it here. What I need is "holiDays" to get the value of "Globalna"
0

you must be modify your function code like this

function ratko(a) {
  var k = a.toString();
  var z = k.replace(/\,/g, '], [');
  var s = z.replace(/./g, ', ');
  var final = "[[" + s + "]]";
  //alert(final); comment this code line and replace this with the code above
  return final;
}

after you can call your function ratko with this simple code

var inputValue = 'simple message';
var parsedValue = ratko(inputValue);

you find the final value into a new variable parsedValue

7 Comments

Again sir, on document.write(parsedValue) i get "undefined"
but your alert return "undefined"? in this case the problem is the function... another question is if you use the document.write under the call at the function or in another context
No, the alert giver me everything fine, but the document.write - doesn't
you use the document.write under the call at the function or in another context?
no, outside of the function. I use it for sole purpose to see if my variable gets the data.
|

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.