I am curious about the distinction between declaring a variable using var
. To explore this, I tried the following code:
<body>
<h1>New Web Project Page</h1>
<script type="text/javascript">
function test(){
a = "hello";
var b="world";
}
alert(a);
alert(b);
</script>
</body>
I am puzzled as to why the alerts are not working and what exactly is the difference when variables are declared with or without the use of var
in JavaScript.