JavaScript global variable definition inside a function
<p>I have a global variable <code>a</code> and I am using it inside a function and assigning a value to it. When I use this variable outside the function it gives me <code>undefined</code>.</p>

<p>Eg.:</p>

<pre><code>var a;
function my_func(){
  a=5;
}
console.log(a); //outputs undefined, how do I get the value 5 here
</code></pre>

<p>Why do I get <code>undefined</code> instead of <code>5</code>?</p>

<hr>

<p>It dint resolve my issue.</p>

<pre><code>var id;

function set_id(myid){
 id=myid;
}

function get_id(){
 return id;
}

$("#btn").click(function(){
 $.post("....", function(data){ //data reurns a JSON
  set_id(id); //success!!
 }
}

$("#show").click(function()[
 console.log(get_id()); //doesn't work, how do I get this workin.. Where am I going wrong
}
</code></pre>
