How to create Statements in Javascript

Description

Javascript statements are terminated by a semicolon.

Or you can omit the semicolon and let the parser determine where the end of a statement is.

Including semicolons helps prevent errors.

Example

The first line of code has no semicolon. It leaves the parser to decide.


var sum = a + b        //valid even without a semicolon - not recommended
var diff = a - b;      //valid - preferred

Block

Multiple statements can be combined into a code block by using C-style syntax, beginning with { and ending with }:


if (test){
   test = false;
   console.log(test);
}

The best practice is to use code blocks with control statements, even if there's only one statement.


if (test)
   alert(test);     //valid, but error-prone and should be avoided

if (test){           //preferred
   console.log(test);
}





















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions