<p>The following code illustrates an object literal being assigned, but with no semicolon afterwards:</p>

<pre><code>var literal = {
    say: function(msg) { alert(msg); }
}
literal.say("hello world!");
</code></pre>

<p>This appears to be legal, and doesn't issue a warning (at least in Firefox&nbsp;3). Is this completely legal, or is there a strict version of JavaScript where this is not allowed?</p>

<p>I'm wondering in particular for future compatibility issues... I would like to be writing "correct" JavaScript, so if technically I need to use the semicolon, I would like to be using it.</p>

[{'answer_id': 42252, 'author': 'Jason Bunting', 'author_id': 1790, 'author_profile': 'https://Stackoverflow.com/users/1790', 'pm_score': 6, 'selected': True, 'text': '<p>Not technically, JavaScript has semicolons as optional in many situations. </p>\n\n<p>But, as a general rule, use them at the end of any statement. Why? Because if you ever want to compress the script, it will save you from countless hours of frustration.</p>\n\n<p>Automatic semicolon insertion is performed by the interpreter, so you <em>can</em> leave them out if you so choose. In the comments, someone claimed that </p>\n\n<blockquote>\n  <p>Semicolons are not optional with statements like break/continue/throw</p>\n</blockquote>\n\n<p>but this is incorrect. They are optional; what is really happening is that line terminators affect the automatic semicolon insertion; it is a subtle difference. </p>\n\n<p>Here is the rest of the standard on semicolon insertion:</p>\n\n<blockquote>\n  <p>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.</p>\n</blockquote>\n'}, {'answer_id': 42256, 'author': 'Geoff', 'author_id': 1097, 'author_profile': 'https://Stackoverflow.com/users/1097', 'pm_score': 0, 'selected': False, 'text': '<p>Use <a href="http://www.jslint.com/" rel="nofollow noreferrer">JSLint</a> to keep your JavaScript clean and tidy</p>\n\n<p>JSLint says: </p>\n\n<blockquote>\n  <p>Error:</p>\n  \n  <p>Implied global: alert 2</p>\n  \n  <p>Problem at line 3 character 2: Missing\n  semicolon.</p>\n  \n  <p>}</p>\n</blockquote>\n'}, {'answer_id': 42259, 'author': 'Kamiel Wanrooij', 'author_id': 4174, 'author_profile': 'https://Stackoverflow.com/users/4174', 'pm_score': -1, 'selected': False, 'text': '<p>This is not valid (see clarification below) JavaScript code, since the assignment is just a regular statement, no different from</p>\n\n<pre><code>var foo = "bar";\n</code></pre>\n\n<p>The semicolon can be left out since JavaScript interpreters attempt to add a semicolon to fix syntax errors, but this is an extra and unnecessary step. I don\'t know of any strict mode, but I do know that automated parsers or compressors / obfuscators need that semicolon.</p>\n\n<p>If you want to be writing correct JavaScript code, write the semicolon :-)</p>\n\n<p>According to the ECMAscript spec, <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" rel="nofollow noreferrer">http://www.ecma-international.org/publications/standards/Ecma-262.htm</a>, the semicolons are automatically inserted if missing. This makes them not required for the script author, but it implies they are required for the interpreter. This means the answer to the original question is \'No\', they are not required when writing a script, but, as is pointed out by others, it is recommended for various reasons.</p>\n'}, {'answer_id': 42269, 'author': 'Travis', 'author_id': 4284, 'author_profile': 'https://Stackoverflow.com/users/4284', 'pm_score': 0, 'selected': False, 'text': '<p>The semi-colon is not necessary. Some people choose to follow the convention of always terminating with a semi-colon instead of allowing JavaScript to do so automatically at linebreaks, but I\'m sure you\'ll find groups advocating either direction.</p>\n\n<p>If you are looking at writing "correct" JavaScript, I would suggest testing things in Firefox with <code>javascript.options.strict</code> (accessed via <code>about:config</code>) set to true. It might not catch everything, but it should help you ensure your JavaScript code is more compliant.</p>\n'}, {'answer_id': 42311, 'author': 'Daniel James', 'author_id': 2434, 'author_profile': 'https://Stackoverflow.com/users/2434', 'pm_score': 4, 'selected': False, 'text': "<p>The YUI Compressor and dojo shrinksafe should work perfectly fine without semicolons since they're based on a full JavaScript parser. But Packer and JSMin won't.</p>\n\n<p>The other reason to always use semi-colons at the end of statements is that occasionally you can accidentally combine two statements to create something very different. For example, if you follow the statement with the common technique to create a scope using a closure:</p>\n\n<pre><code>var literal = {\n    say: function(msg) { alert(msg); }\n}\n(function() {\n    // ....\n})();\n</code></pre>\n\n<p>The parser might interpret the brackets as a function call, here causing a type error, but in other circumstances it could cause a subtle bug that's tricky to trace. Another interesting mishap is if the next statement starts with a regular expression, the parser might think the first forward slash is a division symbol.</p>\n"}, {'answer_id': 42317, 'author': 'TonyLa', 'author_id': 1295, 'author_profile': 'https://Stackoverflow.com/users/1295', 'pm_score': 2, 'selected': False, 'text': '<p>In this case there is no need for a semicolon at the end of the statement.  The conclusion is the same but the reasoning is way off.</p>\n\n<p>JavaScript does not have semicolons as "optional". Rather, it has strict rules around automatic semicolon insertion. Semicolons are not optional with statements like <code>break</code>, <code>continue</code>, or <code>throw</code>. Refer to the <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" rel="nofollow noreferrer">ECMA Language Specification</a> for more details; specifically <a href="http://www.ecma-international.org/ecma-262/7.0/index.html#sec-rules-of-automatic-semicolon-insertion" rel="nofollow noreferrer">11.9.1, rules of automatic semicolon insertion</a>.</p>\n'}, {'answer_id': 42510, 'author': 'JesDaw', 'author_id': 4440, 'author_profile': 'https://Stackoverflow.com/users/4440', 'pm_score': 3, 'selected': False, 'text': '<p>JavaScript interpreters do something called "semicolon insertion", so if a line without a semicolon is valid, a semicolon will quietly be added to the end of the statement and no error will occur.</p>\n\n<pre><code>var foo = \'bar\'\n// Valid, foo now contains \'bar\'\nvar bas =\n    { prop: \'yay!\' }\n// Valid, bas now contains object with property \'prop\' containing \'yay!\'\nvar zeb =\nswitch (zeb) {\n  ...\n// Invalid, because the lines following \'var zeb =\' aren\'t an assignable value\n</code></pre>\n\n<p>Not too complicated and at least an error gets thrown when something is clearly not right. But there are cases where an error is <em>not</em> thrown, but the statements are not executed as intended due to semicolon insertion. Consider a function that is supposed to return an object:</p>\n\n<pre><code>return {\n    prop: \'yay!\'\n}\n// The object literal gets returned as expected and all is well\nreturn\n{\n    prop: \'nay!\'\n}\n// Oops! return by itself is a perfectly valid statement, so a semicolon\n// is inserted and undefined is unexpectedly returned, rather than the object\n// literal. Note that no error occurred.\n</code></pre>\n\n<p>Bugs like this can be maddeningly difficult to hunt down and while you can\'t <em>ensure</em> this never happens (since there\'s no way I know of to turn off semicolon insertion), these sorts of bugs are easier to identify when you make your intentions clear by consistently using semicolons. That and explicitly adding semicolons is generally considered good style.</p>\n\n<p>I was first made aware of this insidious little possibility when reading <a href="http://www.crockford.com/" rel="nofollow noreferrer">Douglas Crockford\'s</a> superb and succinct book "<a href="https://rads.stackoverflow.com/amzn/click/com/0596517742" rel="nofollow noreferrer" rel="nofollow noreferrer">JavaScript: The Good Parts</a>". I <em>highly</em> recommend it.</p>\n'}]
['https://Stackoverflow.com/questions/42247', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/122/']