0

Is there any how javascript or jquery to store values in the given variables while interacting through functions wuthout using global vars?

I made an example: http://jsfiddle.net/1fw2urks/3/

$(document).ready(function(){
    start();
});


function start(){
    var n = 0;
    for (i = 0; i < 10; i++){
        count(n);   
    }
    alert(n);
}

function count(n){
    countAgain(n);   
}

function countAgain(n){
    n += 1;
}

n var should come with value 10 if it worked, but it seems that we can't change the passed parameters value while in the function.

Is this affirmation right? (I did some research and i found this answer, but, I am stubborn)

  • The argument is a primitive and is passed as a value, making it a new local variable to each function etc. so you can't change the local variable/argument in each function and expect it to propogate, you have to return the values -> **http://jsfiddle.net/1fw2urks/4/** – adeneo Feb 27 '15 at 16:27
  • Thanks for the answer Adeneo, i already have a return statement, so, this would not be the best solution, ill have to use global variables (which i am not very fond of). –  Feb 27 '15 at 17:48

0 Answers0