Reliable JavaScript Certification Online #

The basic syntax of JavaScript is similar to the syntax C-family languages, mostly follows Java language, also draws from Perl, Python and Awk.

  1. Identifiers
  2. Whitespace
  3. Statements
  4. Comments
  5. Case-sensitive

Identifiers

Identifier in JavaScript is name of variable, constant, property, function or function parameters.

It can consists of one or more characters. First character must be letter, dollar sign ($), or underscore (_). Subsequent characters may be letters, numbers, dollar signs and underscores.

JavaScript uses the Unicode character set, although recommended is to use characters from the ASCII subset.

Keywords and reserved words cannot be used as identifiers.

 

Whitespace

Whitespace are characters such as spaces, tabs and new line characters. Whitespace is usually insignificant or its role is to separate sequences of characters which without whitespace would be merged into one token.

var x =     10 ;

var x=10;

This two lines are equivalent, all redundant whitespace in the first line can be removed, only whitespace between var and x can not be removed.

For code readability it is recommended to use one space between separate sequences:

var x = 10;

 

Statements

Statements are instructions and are separated by a semicolon (;).

Example of variable declaration statement:

var text = "JavaScript";

Semicolon is not mandatory at the end of statements. When it is not provided then parser determines where the end of statement occurs. It is recommended to always put semicolon at the end of statement, that makes code more clear, helps to avoid errors and allows code compression.

The JavaScript source code is parsed from left to right and is converted into sequence of input elements (tokens, whitespace, control characters, comments, line terminators).

Normally statements are executed from top to bottom but the order of execution can be changed by control flow statements or function invocation.

Often there is a need to use multiple statements together. For this purpose, a code block is used. The code block is a set of statements wrapped in curly brackets ({}):

if (x < 10) {
    x = x + 1;
    sum = sum + x;
}

Control statements require code blocks when they apply to multiple statements, for single statement it is not obligatory:

if (x > 10)
    x = x - 1;

However it is recommended to always use code blocks with control statements. It makes code more clear and less prone to errors.

 

Comments
Comments style is the same like in other C-family languages.
A single line comment begins with two forward-slashes (//):

// A sing line comment

A longer, multi-line comment is wrapped by forward slash and asterisk (/*) at the beginning and asterisk and forward slash (*/) at the end:

/*

Here is a longer

multi-line comment

*/

 

Case-sensitive
JavaScript is case-sensitive, this applies to variables, functions, operators and it means that upper case and lower case text is treated as distinct. Eg. variables named textarea and textArea are different.