I'm learning JavaScript. What does this mean, exactly?
You might see examples without semicolons. Ending statements with semicolon is optional in JavaScript.
I'm learning JavaScript. What does this mean, exactly?
You might see examples without semicolons. Ending statements with semicolon is optional in JavaScript.
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.
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.