1

I'm learning JavaScript. What does this mean, exactly?

You might see examples without semicolons. Ending statements with semicolon is optional in JavaScript.

From http://www.w3schools.com/js/js_statements.asp

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • 6
    http://w3fools.com You should learn from https://developer.mozilla.org/en-US/docs/Web/JavaScript – SLaks Feb 23 '14 at 03:52
  • It is optional but a good idea to do so – Ed Heal Feb 23 '14 at 03:52
  • http://www.codecademy.com/blog/78-your-guide-to-semicolons-in-javascript – j08691 Feb 23 '14 at 03:54
  • 2
    [optional](http://www.merriam-webster.com/dictionary/optional): available as a choice but not required. It means you can choose whether to do it. – user2357112 Feb 23 '14 at 03:55
  • ["The moral of this story: ASI is (formally speaking) a syntactic error correction procedure."](https://brendaneich.com/2012/04/the-infernal-semicolon/). – Andy Feb 23 '14 at 04:42

2 Answers2

1

JavaScript uses a C-like syntax which requires the use of semicolons to delimit certain statements. JavaScript attempts to make those semicolons optional with a semicolon insertion mechanism. This is dangerous because it can mask errors.

Like C, JavaScript has ++ and -- and ( operators which can be prefixes or suffixes. The disambiguation is done by the semicolon.

In JavaScript, a linefeed can be whitespace or it can act as a semicolon. This replaces one ambiguity with another.

via- http://www.jslint.com/lint.html

Akhlesh
  • 2,389
  • 1
  • 16
  • 23
1

As per the ECMAScript® Language Specification's Automatic Semicolon Insertion section,

Certain ECMAScript statements (empty statement, variable statement, expression statement, do-while statement, continue statement, break statement, return statement, and throw statement) must be terminated with semicolons. Such semicolons may always appear explicitly in the source text. For convenience, however, such semicolons may be omitted from the source text in certain situations. These situations are described by saying that semicolons are automatically inserted into the source code token stream in those situations.

Hope this directly answers your question.

Suggestion: But use your best judgement and add the semi-colon yourself whenever possible.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497