Changes to variable value don't persist in if statement in JavaScript - Stack Overflow

I'm setting the value of a variable inside the setAndCheck function to 1 by calling it with define

I'm setting the value of a variable inside the setAndCheck function to 1 by calling it with defined string. The value is set, but when I call the function later to check the value later, it seems like that the change has not been applied and therefore the related if statement is not executed.

The job of the function is to assign 1 to argument if called with appropriate and then it's called to check whether it's been set and return 1. If it's not then it just returns 0 at the end.

function setAndCheck(arg) {
  var first_one = 0;
  var second_one = 0;
  if (arg == "setIt1")
    first_one = 1;
  else if (arg == "setIt2")
    second_one = 1;
  else if (arg == "checkIt1" && first_one)
    return 1;
  else if (arg == "checkIt2" && second_one)
    return 1;
  return 0;
}

I expected the output of 1 after setting both variables with "setIt1" and "setIt2". But the function still returns 0 which means the variables have the value of 0 when called with "checkIt1" or "CheckIt2".

I'm setting the value of a variable inside the setAndCheck function to 1 by calling it with defined string. The value is set, but when I call the function later to check the value later, it seems like that the change has not been applied and therefore the related if statement is not executed.

The job of the function is to assign 1 to argument if called with appropriate and then it's called to check whether it's been set and return 1. If it's not then it just returns 0 at the end.

function setAndCheck(arg) {
  var first_one = 0;
  var second_one = 0;
  if (arg == "setIt1")
    first_one = 1;
  else if (arg == "setIt2")
    second_one = 1;
  else if (arg == "checkIt1" && first_one)
    return 1;
  else if (arg == "checkIt2" && second_one)
    return 1;
  return 0;
}

I expected the output of 1 after setting both variables with "setIt1" and "setIt2". But the function still returns 0 which means the variables have the value of 0 when called with "checkIt1" or "CheckIt2".

Share Improve this question edited May 9, 2019 at 7:45 Ivar 6,90712 gold badges56 silver badges67 bronze badges asked May 9, 2019 at 7:34 AllerterAllerter 171 silver badge6 bronze badges 4
  • Its not exactly clear what you are pointing out, but you are initialising the variables first_one and second_one to 0 every time you call the function – Krishna Prashatt Commented May 9, 2019 at 7:36
  • Every single time you call the function, var first_one = 0 is executed again as well… – deceze Commented May 9, 2019 at 7:36
  • It's not if statements - executing a function runs the entire code again. So you'd initialise a new variable called first_one and go through the if chain - on the first go you'd hit the arg == "setIt1" condition. When you execute the function again, you would initialise a new variable and hit the arg == "checkIt1" && first_one condition which would evaluate to false, since you never went through the arg == "setIt1" condition this time; – VLAZ Commented May 9, 2019 at 7:36
  • That's not how local variables work. Variables defined within functions cease to exist once the function exits. (this isn't exactly how it works but is close enough for your purposes. – phuzi Commented May 9, 2019 at 7:38
Add a ment  | 

3 Answers 3

Reset to default 4

Every time you define a variable inside a function, that variable gets reset whenever you call that function again since you are redeclaring it. To make the variable persist you need to make it available in the global-scope by putting it outside your function.

     var first_one = 0;
     var second_one = 0;

    function setAndCheck(arg){
      if (arg == "setIt1")
      first_one=1;
      else if (arg == "setIt2")
      second_one=1;
      else if (arg == "checkIt1" && first_one)
        return 1;
      else if (arg == "checkIt2" && second_one)
        return 1;
      return 0;
    }

I call the function later to check the value later, it seems like that the change has not been applied and therefore the related if statement is not executed.

Every time the function will b e called it will again create the variable. So the value will never be saved.

You can create the variable outside the function and then assign the value. Next time you need not to call the function but directly use the value

Well, as far as I can see, the problem lies in the fact that you declare and initialize the variables inside the scope of the function.

Let's take it from the beginning.

  1. The function is called with arg "setIt1". first_one and second_one are set to 0, then first_one is set to 1.
  2. The function is called with arg "setIt2". first_one and second_one are reset to 0, then second_one is set to 1.
  3. The function is called with arg "checkIt1. first_one and second_one are reset to 0, so (arg == "checkIt1" && first_one) returns false, thus resulting in the function returning 0.

edit: The second problem is that local variables (variables declared inside a function cease to exist when the function ends. So every time the function is executed and exits, both first_one and second_one disappear. The only way to keep the values is to declare first_one and second_one before calling the function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745327352a4622720.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信