Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access variable outside function scope [duplicate]

This is a simplified version of what I am trying to accomplish, but I want to pass a variable outside the scope of the function. I am declaring the variable outside the function but can't get it.

HTML:

<p>5</p>
<p>6</p>
<p>7</p>

JS:

$(document).ready(function () {
    var gsd = "";
    $("p").each(function () {
        if ($(this).text() === "5") {
            var gsd = $(this).text();
            alert(gsd); // this works
        }
    })
    alert("get var outside func" + gsd); //does not work
});
like image 591
user2232681 Avatar asked Jun 05 '13 14:06

user2232681


People also ask

What happens when you try to access a variable outside of its scope?

It will throw an error if you try to access a variable which is not in the local or global scope.

Can you access the Let variable outside of the block scope?

No, You can't access let variables outside the block, if you really want to access, declare it outside, which is common scope.

Can you access a variable outside of a for loop?

If the variable is declared outside the loop, then it has the global scope as it can be used through-out the function and inside of the loop too. If the variable is declared inside the loop, then the scope is only valid inside the loop and if used outside the loop will give an error.

What is the difference between LET and VAR?

let is block-scoped. var is function scoped. let does not allow to redeclare variables. var allows to redeclare variables.

What is variable scope in MS Access VBA?

Variable Scope in VBA for MS Access. This replaces the old use of Global as a variable declaration. If the module concerned is a Form module, the variable is restricted to the code region of the form in which it is declared. You cannot declare a public variable within a procedure. This will give an error.

What is the scope of a variable in a module?

When a variable is declared within a module but outside of any procedure then it is available thoughout the module. For Form modules the scope is limited to the code region of the form. For standard modules the limit of scope will depend on the type of declaration. If you Dim a variable in a module it will default to Private.

What are the different types of variable scopes in JavaScript?

There are mainly two types of variable scopes: Now let’s understand each of the scope at a greater detail: Variables defined within a function or block are said to be local to those functions. Anything between ‘ {‘ and ‘}’ is said to inside a block.

What is the scope of a form module?

For Form modules the scope is limited to the code region of the form. For standard modules the limit of scope will depend on the type of declaration. If you Dim a variable in a module it will default to Private.


1 Answers

You redeclare gsd as a new variable inside your function. Remove var in front of gsd inside the function to address the gsd in the outer scope.

like image 162
Igor Avatar answered Oct 04 '22 13:10

Igor