Possible Duplicate:
Difference between using var and not using var in JavaScript
What is the primary difference between these two approaches. Could someone help me understand. What does it mean if a variable is declared with "var" keyword inside a function?? in both the cases the timer is cleared in 3secs as expected..
Approach 1:
<html>
<body>
<script type="text/javascript">
function function2()
{
int=self.setInterval(function(){
alert(int);
clearInterval(alert("in clear"+ int));
},3000);
}
</script>
<button onclick="function2()">Start</button>
</body>
</html>
Approach 2
<html>
<body>
<script type="text/javascript">
function function2()
{
var int=self.setInterval(function(){
alert(int);
clearInterval(alert("in clear"+ int));
},3000);
}
</script>
<button onclick="function2()">Start</button>
</body>
</html>