Javascript - Statement

About

An expression produces a value whereas a statement will not.

Syntax

All statements in JavaScript must end with a semi-colon, to indicate that this is where the statement ends.

The parser may add semicolon if it encounter a parse error. See below Automatic semicolon insertion

Example

The statement

b
(2);

is equivalent to

b(2);

Groupement

statements can be grouped in a block

Automatic semicolon insertion

Ther parser infers omitted semicolons in certain contexts, effectively “inserting” the missing semicolon into the program for you automatically. The ECMAScript standard precisely specifies the semicolon insertion mechanism.

Parse Error correction

Parser Rules:

  1. Semicolons are only ever inserted before a } token, after one or more newlines, or at the end of the program input.
  2. Semicolons are only ever inserted when the next input token cannot be parsed. In other words, semicolon insertion is an error correction mechanism.

To avoid an unwanted automatic semicolon insertion problem, you can prefix statements beginning with (, [, +, -, or / with an extra semicolon.

Except that semicolons are never inserted as separators in the head of a for loop or as empty statements.

Restricted production

Where no newline is allowed to appear between two tokens, Javascript will insert a semicolon thanks to the restricted productions

The restricted productions are:

  • throw
  • break or continue statement with an explicit label
  • a postfix ++ or -- operator
  • return

Example with:

  • return:
function returnValueOnSameLine(){
    return 4;
}

function returnValueOnNextLine(){
    return // The javascript parser will add here a semicolon because the return keyword is not allowed to spread on multi-line.
    4;
}

console.log(returnValueOnSameLine());
console.log(returnValueOnNextLine());
  • Postfix. ++ (or %–%) can serve as either a prefix or a suffix, but only the suffix cannot be preceded by a newline.
a=0;
b=0;

a
// As the suffix cannot be preceded by a new line, ++ will be a prefix and the parser add here a semicolon
++ 
b

console.log(a);
console.log(b);

Documentation / Reference





Discover More
Javascript - Arrow Function Expression

An arrow function expression has a shorter syntax than a function expression and does not bind its own: this, arguments, super, or new.target. These function expressions are best suited for...
Javascript - Code block {}

in Javascript {} will group statement
Javascript - Expression

Javascript language expression: An expression produces a value whereas a statement will not. And therefore, expressions can be: passed where a variable is expected such as as function argument, assignment...
Javascript - Grouping Operator ()

Grouping Operator () for expression. function signature Parens () can’t contain statements.
Javascript - Special Characters

Characters that can act as: Expression operator (ie an operator) The beginning of a statement ( [ + Concat or addition - Minus / Division Regular expression Example: / as a regular...
React - JSX

JSX is a Javascript HTML Like language that compiles to Javascript React API. The first motivation of JSX is that reading and writing React elements, are easier. It permits to: write JSX elements...



Share this page:
Follow us:
Task Runner