tags:

views:

78

answers:

6
for(var i = 0, var p = ''; i < 5; i++)
{
    p += i;
}

Based on a JavaScript book i'm reading, this is valid code. When I test it doesn't work, and in FireBug I get this error:

SyntaxError: missing variable name
+8  A: 
var i = 0, var p = '';

should be

var i = 0, p = '';

the var keyword applies to the whole line.

David Dorward
thanks; it was my fault, not the book author's :P
Chad
+2  A: 
var p = 0;
var i = 0;


for(i = 0; i < 5; i++)
{
    p += i;
}

or

for(var i = 0, p = 0; i < 5; i++)
{
    p += i;
}
gmcalab
+2  A: 

remove the var from before p = ''.

Marius
+4  A: 

It looks like a typo.

You need to remove the second var and it will work perfectly:

for(var i = 0, p = ''; i < 5; i++)
{
    p += i;
}
Daniel Vassallo
+1  A: 

Don't repeat the var, you only need it once in the declaration:

for (var i = 0, p = ''; i < 5; i++)
{
    p += i;
}
Brian Campbell
+1  A: 

you can't declare a variable in the second position termitation expression is the following works

var p;
for(var i = 0, p = ''; i < 5; i++)
{
    p += i;
}
fuzzy lollipop