Skip to content
On this page

Basic Syntax

The syntax of JavaScript is similar to that of the Java language, with each statement ending with a semicolon (😉 and statement blocks enclosed in {...}. However, JavaScript does not enforce the requirement to add a semicolon at the end of each statement; the engine responsible for executing JavaScript code in the browser automatically adds a semicolon at the end of each statement.

Tip

Allowing the JavaScript engine to automatically add semicolons can, in some cases, change the semantics of the program, leading to results that differ from expectations. In this tutorial, we will not omit semicolons; all statements will include them.

For example, the following line of code is a complete assignment statement:

javascript
var x = 1;

The following line of code is a string but can still be considered a complete statement:

javascript
'Hello, world';

The following line of code contains two statements, each ending with a semicolon:

javascript
var x = 1; var y = 2; // It's not recommended to write multiple statements on one line!

A statement block is a collection of statements. For example, the following code first performs a check; if the condition is true, it will execute all statements within {...}:

javascript
if (2 > 1) {
    x = 1;
    y = 2;
    z = 3;
}

Note that the statements within the curly braces {...} are indented, usually with four spaces. Indentation is not a strict requirement of JavaScript syntax, but it helps us understand the structure of the code, so it’s important to adhere to indentation rules when writing code. Many text editors have an "auto-indent" feature to help organize code.

Curly braces {...} can also be nested, forming a hierarchical structure:

javascript
if (2 > 1) {
    x = 1;
    y = 2;
    z = 3;
    if (x < y) {
        z = 4;
    }
    if (x > y) {
        z = 5;
    }
}

JavaScript itself places no limits on the level of nesting; however, excessive nesting undoubtedly makes it much harder to understand the code. In such cases, some code should be extracted and defined as functions to reduce complexity.

Comments

Characters starting with // and continuing to the end of the line are treated as line comments, intended for developers to read, which the JavaScript engine automatically ignores:

javascript
// This is a line comment
alert('hello'); // This is also a comment

Another type of comment is the block comment, which wraps multiple lines of text using /.../, treating the entire block as a single comment:

javascript
/* This starts a block comment
It is still a comment
It is still a comment
Comment ends */

Exercise

Use line comments and block comments to comment out the statements below so they no longer execute:

javascript
// Please comment out the statements below:
alert('I do not want to execute');
alert('I also do not want to execute');

Case Sensitivity

Note that JavaScript is case-sensitive; if you get the casing wrong, the program will throw an error or not run correctly.

Basic Syntax has loaded