2

Possible Duplicate:
Do you recommend using semicolons after every statement in JavaScript?

I have the code like this:

$("#delete_" + row)
   .attr('data-href', '/Admin/' + tab + 's/Delete' + params)
   .attr('title', 'Delete ' + id);

It's typical of the code I have everywhere in my application. What I notice is that it seems to work with or without a semicolon at the end. Is there any advantage for formatting or any other reason that I should put a semicolon at the end?

Community
  • 1
  • 1
  • It is unlikly good question for SO as it is more or less codnig style preference. JavaScript is very forgiving to usage/non-usage of semicolon... So learn where it will automatically insert one and see what works for your coding style. – Alexei Levenkov Sep 25 '12 at 04:17
  • Don't as us, ask Lint, http://www.jslint.com – patrickmcgraw Sep 25 '12 at 04:20

5 Answers5

4

Always put the semicolon. Not only is it more cross-browser compatible, it's easier to minify (removing newlines is part of that).

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • Thank you very much for your advice and thanks to everyone else. I upvoted everyone and selected your answer as you were first. Some great advice about mimify –  Sep 25 '12 at 04:32
1

Semicolons are not required in Javascript, however, it is strongly advised to put them in instead of relying on the parser to do the right thing in certain situations

Hazem Salama
  • 14,891
  • 5
  • 27
  • 31
0

JavaScript supports automatic semi-colon insertion, but it's generally recommended to avoid it.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
0

Semicolons, although not required in JavaScript, denote end of statement. So its a good habit of ending a statement with semicolon at the end. Other point, Explosion Pills has already mentioned.

Starx
  • 77,474
  • 47
  • 185
  • 261
0

Yes, you should use semicolons after every statement in JavaScript.

They are not needed, but I personally use them because most minification techniques rely on the semi-colon

This answer and its opposite are intended as a poll. Vote for the one you agree with, and then post an answer with your reasons if you like.

Community
  • 1
  • 1
chhameed
  • 4,406
  • 4
  • 26
  • 44