0

I want to define a variable (Boolean), and then use it in a function two times. I started with defining a global variable, and then use this variable locally in my function as follows:

var  inpLock = false;
…
function doSomething(inpLock) {
    inpLock = true;
    switch …
        case
        case
    inpLock = false;
}

What happens by running this is: it sets the variable to true but not back to false. If I declare the variable inside the function like: var inpLock it also won’t work. Any help will be appreciated.

1
  • Can you reproduce it and give a link of jsfiddle of same thing? Commented Aug 7, 2017 at 11:39

1 Answer 1

1

Giving a name to a function argument (doSomething(inpLock)) declares a local variable of that name.

This masks any global variable with the same name.

Changes you make to the variable inside the function only touch the local variable and ignore the global.

Avoid reusing variable names in nested scopes. It causes confusion.

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

2 Comments

How can I assign a value to a variable in a function and then access this value globally?
@GhzJabb — (1) Stop creating a local variable with the same name. (2) Use its name.

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.