What are the dangers of breaking a line of long code up at a close parenthesis?
When could a semicolon be automatically inserted by Javascript (pres
When you're trying to return an object,
return {
'foo': 'bar'
}
will return the object, whereas
return
{
'foo': 'bar'
}
will return undefined. Javascript will automatically insert a semicolon after return in the second example, and the object will never be reached.
For functions, because function() isn't valid on its own, it shouldn't make a difference if the brace is on the same line or the next.
See also section 7.9.1 Rules of Automatic Semicolon Insertion of the ECMAScript specification. Aside from return, there are four other situations where a semicolon will be inserted due to a newline: break, continue, throw and ++ or --.
When a
continue,break,return, orthrowtoken is encountered and a LineTerminator is encountered before the next token, a semicolon is automatically inserted after thecontinue,break,return, orthrowtoken.