JavaScript/Automatic semicolon insertion
< JavaScriptThe automatic semicolon insertion (ASI)
In C-like languages, the semicolon denotes the end of a statement. Unlike other C-like languages, JavaScript does not always enforce the use of a semicolon at the end of a statement. Rather, a carriage return may be "understood" as a missing semicolon, resulting in different semantics.
In JavaScript, a semicolon is automatically inserted when [1]
- two statements are separated by a line terminator
- two statements are separated by a closing brace ('}')
- a line terminator follows a break, continue, return, or throw.
Examples
Entered code | "Understood" as |
---|---|
return 2*a + 1; | return; 2*a + 1; |
function getObject() { return { // some lines }; } | function getObject() { return; { // some lines }; } |
i ++; | i; ++; |
if (i === 5) // assuming a semicolon here else foo = 0; | if (i === 5) // no semicolon here! else foo = 0; |
References
- ↑ cjihrig (2012-03-09). "The Dangers of JavaScript’s Automatic Semicolon Insertion" (in English) (HTML). cjihrig.com. Archived from the original on 2012-03-09. http://cjihrig.com/blog/the-dangers-of-javascripts-automatic-semicolon-insertion/. Retrieved 2015-04-03.
See also
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.