Cool web site - A Automatic semi-colon insertion (Definition) There are

A Automatic semi-colon insertion (Definition) There are cases where the the interpreter will automatically insert semi-colons as needed. You won’t see them in the script source, but the interpreter knows they should be there. You should not rely on the interpreter doing your work for you. For example, semi-colons are never added inside for statement headers. Here are some instances of how the browser deals with automatic semi-colon insertion: . Semi-colons are automatically placed before curly braces (}) that close code blocks if necessary. . A semi-colon is added at the end of a script source text if necessary to parse the source as a complete program. . Semi-colons are added to prevent accidental postfix increment or decrement operations. Postfix ++ or — operators should be on the same line as the operand to which they apply. Actually it is good practice for there to be no whitespace between them. . Semi-colons are added after the return statement when it is the last statement on a line. An expression to be evaluated as part of a return statement should be placed adjacent to it. It is good practice to form the return as if it were a function, enclosing the expression in parentheses: return(expression); . This is unaffected by automatic semi-colon insertion even though it is syntactically incorrect: for (a; b) . This is transformed: returna + b And becomes: return;a + b; However, a + b is not returned as a result because the line terminator separates them from the return statement. . People take a great many liberties with the formatting of if else constructions. This won’t get fixed: if(a > b)else c = d . This won’t get fixed either: a = b + c(d + e).print() It doesn’t get fixed because the parentheses look like a function call. Warnings: . Careful programmers always put semi-colons in. If you come from a C or Java background, this may be instinctive, but otherwise you should develop the habit so that it becomes instinctive. See also: Free-format language, Lexical convention, Line terminator, Semi-colon (;)

Leave a Reply