JavaScript Grammar

The syntax used by JavaScript is known as a C-style syntax, which is similar to the one used by Java.
A JavaScript program is made up of a series of statements. Each statement ends with a new line or semicolon.

Here is an example of two statements, one on each line:
a = "Hello World!"
alert(a)

This example could also be written as follows, using semicolons at the end of each statement:
a = "Hello World!";alert(a);  

There’s no need to actually use a semicolon to terminate a statement because JavaScript interpreters use a process called Automatic Semicolon Insertion (ASI). This will attempt to place semicolons at the end of lines for you; however, it can be error-prone and cause a number of automated services such as code minifiers and validators to not work properly.

For this reason, it’s considered best practice to combine the two and write each statement on a new line, terminated by a semi-colon, like so:
a = "Hello World!";
alert(a);
 

A block is a series of statements that are collected together inside curly braces:

{
    // this is a block containing 2 statements
    var a = "Hello!";
    alert(a);
}
  

Blocks do not need to be terminated by a semicolon.
Whitespace (such as spaces, tabs, and new lines) is used to separate the different values in each statement You can use as much whitespace as required to format your code so that it is neat and easy to read. Examples of this include using spaces to indent nested code and multiple lines to separate blocks of code.

0 Response to "JavaScript Grammar"

Post a Comment