0

Good day!

I am writing a script as follows:

var prev = 0;

$(document).ready(function(){
    dynamicListOne(length, prev);
});

function dynamicListOne(length, prev){
   length++;
   prev = length;
}

But prev is always 0. How can i assign prev=length?

Thank you in advance

3
  • 1
    Judging from this snippet (which has errors), the prev in function is not the same prev as the global. You passed a copy of it. Commented Dec 2, 2011 at 3:24
  • A function statement without a name is a syntax error. How have you actually defined it? Commented Dec 2, 2011 at 3:24
  • how can i pass not the copy of it? Commented Dec 2, 2011 at 3:28

3 Answers 3

1

Your function takes prev as an argument so inside the function the name points to a local variable instead of the global one. Just don't add it as an argument to your function.

And think carefully if you really really want a global variable.

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

Comments

0

The prev variable in your function signature hides the global prev variable. Choose a different name and it will work :)

Comments

0

There is no need to pass in prev as a parameter

var prev = 0;

$(document).ready(function(){
    dynamicListOne(length);
    console.log(prev)
});

function(length){
   length++;
   prev = length;
}

Comments

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.