Basics

Statement

JavaScript

 

Statement

ECMAScript 5
console.log('statement 1');
console.log('statement 2');
// ...

Statements are similar to sentences in natural languages.

Statement is a complete unit of execution.

Semicolons (;) are optional but recommended in JavaScript Statements.

 

Implicit Semicolon

ECMAScript 5
console.log('statement 1')
console.log('statement 2')
// ...

JavaScript parser inserts semicolons before the line breaks which can form a Statements.

It is recommended to have semicolons.

 

Whitespaces

ECMAScript 5
console  .log (  "statement 1" );

console.log
  ("statement 2");

console.log(
  "statement 3"
);

JavaScript ignores Whitespaces.

Space, Tab, Line-breaks are all considered whitespace.

You can use this as a tool to make your code more readable.

 

Case Sensitivity

ECMAScript 5
var age = 12;

console.log(age);  // 12
Console.Log(age);  // error
console.log(Age);  // error

JavaScript is a Case Sensitive language.

 

Blocks

ECMAScript 5
{
  console.log('statement 1');
  console.log('statement 2');
}

Block is used to group zero or more statements.

Block is indicated by a pair of curly brackets.

Block can be prefixed or postfixed by other keywords like if, while to run them in a specific way.

 

Block Label

ECMAScript 5
myblock: {
  console.log('statement 1');
  console.log('statement 2');
}

Block (Statement) may optionally be labeled.

These labels can then be used by continue and break keywords.

 

Empty Statement

ECMAScript 5
;

An empty statement is used to provide no statement, although JavaScript syntax would expect one.

For example, if you want to run a for loop without a block.