logo
Search:

Login:


Forgot Details? Sign-up

forum >> Programming questions >> HTML / XHTML / HTML5 / CSS

syntax question

Posted Apr 11 2012 at 6:16 PM by
Pete Woodhead (petewdhd)
Code:
      var frame = 0;        var spriteImage               function init(){          setInterval("animate()", 100);          spriteImage = document.getElementById("image");        } // end init
Quote:
Harris, Andy (2010-10-28). HTML, XHTML and CSS All-In-One For Dummies (p. 488). For Dummies. Kindle Edition.

In the code snippet above why isn't there a semi colon after var spriteImage? Since it's in the book and the source code this way I'm guessing it was intentional. Is it because it proceeds the function init() I tried adding a semi colon to see if it would noticeably affect the code and it did not seem to. It just sparked my curiosity.

Pete
AuthorMessage
Andy
Posted: Apr 12 2012 kl. 3:48 PM

In JavaScript, semicolons are optional. I generally put them in, because most C-like syntaxes require them.

However, I must have simply forgotten. (Most likely, I had been programming in Python that day, which does not require or allow semicolons at the end of the line)

Since semicolons aren't required, it triggered no error, so I never saw it.

I think the best practice is to add a semicolon to the end of every line (except the ones that end with a brace.) If you forget (as I did) no harm will be done. It's just a good practice.

-Andy
Pete Woodhead
Posted: Apr 13 2012 kl. 2:07 PM

in the dull recesses of my mind I believe you mentioned that in your book. Thanks
Andy
Posted: Apr 14 2012 kl. 2:44 PM

Apparently, our brains have the same kind of dark recesses.

Keep in touch, my friend...