The SyntaxError is correct. If you check other browsers, you will see that they give the same result.
The problem here is the ';' after the then-branch of the second/inner if. An if-statement has the condition followed by exactly one statement (then then-branch), and then, optionally, the keyword "else" and another single statement (the else-branch). That single statement can be, and often is, a block statement. The block with the two alerts is the then-statment. The following ';' isn't part of that statement (block statements are not terminated by semicolons), so it must be another empty and unrelated statement. I.e., the then-statement was not followed by the "else" keyword, so the if-statement ends there. Ditto for the outer if statement. Then comes an "else" keyword that's not part of any if-statement, which is a syntax error. Best of luck /Lasse On Fri, Jan 28, 2011 at 13:02, PhistucK <[email protected]> wrote: > I have some questions and need some clarifications. > > When I try this code - > <!DOCTYPE HTML> > <html> > <script> > var a = "o"; > var b = "c"; > if (a == "o") > if (b == "b") > { > alert("a is o"); > alert("b is b"); > }; > else > { > alert("a is not o"); > }; > </script> > </html> > > It shows - > Uncaught SyntaxError: Unexpected token else > > Should that error be triggered? can you explain why? > > > > Also, running it without that semicolon - > <!DOCTYPE HTML> > <html> > <script> > var a = "o"; > var b = "c"; > if (a == "o") > if (b == "b") > { > alert("a is o"); > alert("b is b"); > } > else > { > alert("a is not o"); > }; > </script> > </html> > > Shows an alert - "a is not o". > As far as I know, "if" and "else" count as two statements. If they are, the > "else" block is actually part of the first "if" statement. > Am I misinformed, or is it a bug? > > > Thank you for your time! > > ☆*PhistucK* > > -- > v8-users mailing list > [email protected] > http://groups.google.com/group/v8-users -- Lasse R.H. Nielsen [email protected] 'Faith without judgement merely degrades the spirit divine' Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84 -- v8-users mailing list [email protected] http://groups.google.com/group/v8-users
