views:

186

answers:

8

Hi ,i am new to Javascript.

<html>
<body>
<script type="text/javascript">
var x=5;
document.write(x);
document.write("<br />");
var x
document.write(x);
</script>
</body>
</html>

Result is :

       5
       5

so when the second time when its declared x should be undefined but it takes the previous value of x.please explain this?.Whether it has any special advantage? Thanks.

A: 

As far as my understanding of javascript goes, the use of the var keyword is completely optional in the global scope. It's a different story for functions.

When inside a function, use the var keyword to indicate that the variable is local to the function (as opposed to being global by default).

I personally use var in the global scope to show that a variable is being declared and/or utilized for the first time.

You can reference http://www.w3schools.com/js/js_variables.asp for more info.

Kenaniah
no it is not: without specifying `var`, a variable would fall in the global scope.
jldupont
var is used for scope within a function, inside a function if you dont use var it is automatically global.
John Boker
heh - all within the same 20 seconds.
John Boker
@Kenaniah: you should delete your answer or you risk having a bunch of down-vote really soon...
jldupont
My apologies. I forgot to mention it's usage within functions to indicate a local variable (versus global). Although the question pertains to the global scope, I should have included that. Post edited ;-)
Kenaniah
+1  A: 

so when the second time when its declared x should be undefined

What part of the specification says this?

"Undefined behaviour" does not mean "the variable will be undefined".

Anon.
A: 

That second var x is totally superfluous.

Cyril Gupta
A: 

Within the same scope, it is totally unnecessary to "redeclare" a variable.

jldupont
+8  A: 

You aren't really re-declaring the variable.

The variable statement in JavaScript, is subject to hoisting, that means that they are evaluated at parse-time and later in runtime the assignments are made.

Your code at the end of the parse phase, before the execution looks something like this:

var x;
x = 5;

document.write(x);
document.write("<br />");
document.write(x);
CMS
Thanks now i am clear
Warrior
A: 

Also, a programmer might want to use var to localize a variable:

<script>
var x= 'this is global x';
function my_x() {
 var x= 'localized x';
 alert(x);
}
my_x();
alert(x);
</script>
pygorex1
Although this is possible and a very good description of it, I'm not sure that this is a very good practice to get into, variable names shouldn't generally be repeated with different uses.
Jay
A: 

You should never redeclare a variable within the same scope, if you really want to change this then assign to it. Redeclaration is not required to create a different object in this dynamic language, if you want x to be a string just assign:

x = "hello";

It is not required that you set it back to undefined or redeclare first.

Please note that changing the variable type is not very good practice in most situations, simply stating that it is a possibility if that's what you require.

Jay
+2  A: 

var alone does not perform an assignment. It only flags that when you use the variable name throughout the scope in which the var occurs, you are talking about a local variable and not global (the controversial default). The var is spotted when the function is parsed and holds throughout that scope, so where you put it is irrelevant:

var a= 0;

function foo() {
    a= 1;
    return a;
    var a;
}

var b= foo();
alert('global a='+a+', local a='+b);

Results in global a= 0, local a= 1: even though the var statement is never reached in the course of execution of foo(), it is still effective in making a a local variable.

So declaring var x a second time in the same scope is completely redundant. However you might sometimes still do it, typically when you re-use a local variable name for a second independent use within the same function. Most commonly:

for (var i= 0; i<onething.length; i++) {
    ...do some trivial loop...
}

for (var i= 0; i<anotherthing.length; i++) {
    ...do another trivial loop...
}

Whilst you could certainly omit the second var, and tools like jslint would demand you do so, it might not actually be a good idea.

Imagine you later change or remove the first loop so that it no longer declares i to be var. Now the remaining second loop suddenly changes meaning from a local to a global variable. If you fail to notice when updating the first loop that the second loop has a hidden dependency on it (and you might very well fail to notice that given how the eyes elide the pattern for(...=0 ; ...<...; ...++) into “oh, that's just a standard iterator”), you've got a subtle and annoying-to-debug problem.

bobince