Skip to main content

Syntax Basics

When writing code, it is important not only to know which symbol or construct can be used, but also to understand the terminology and components of the source code. In this section, we will learn the basic terminology and syntax, without studying particular mechanisms.

Statement

Statement is a related set of words and symbols from the syntax of a language that combines to express a common idea, in order to give an instruction to a PC.

a = b * 2;

JavaScript statements end with a semicolon, which can be compared to the period at the end of a sentence in your human language.

  • a and b are variables (as in an algebraic equation), data stores used by the program. A variable consists of an identifier (name) and a related value.
  • 2 is just a number. This is called a literal value because it is not stored in a variable.
  • = and * are operators, they perform actions with values and variables.

Let's imagine that the variable b stores the number 10. Then this instruction tells the machine:

  1. Go find the variable with the identifier b and ask its current value.
  2. Substitute b with the value of the variable b (10).
  3. Perform the operation of multiplying 10 by 2.
  4. Write the result of the right-hand side of the expression to the variable a.
Note

You do not need to end the statement with a semicolon, but we strongly recommend that you always do. This simple rule will make your code clearer and help avoid subtle errors.

Expression

Statements are made up of parts, just like sentences are made up of phrases, and these phrases are called expressions.

Expression is a reference to a variable or value, or to a set of variables and values in combination with operators.

[ [a] = [ [b] * [2] ] ]

The statement from the example above contains 5 expressions, which are in square brackets for visualization (this is not language syntax):

  • [2] is a literal value expression.
  • [b] and [a] are variable expressions; they require to substitute the value of a particular variable, but only if the variable is on right-hand side of the assignment expression.
  • [b * 2] is an arithmetic expression of multiplication.
  • [a = b * 2] is an assignment expression. In our case, it requires to evaluate the right-hand side of the expression and assign the result to the variable a on the left-hand side of the expression.

There are also expressions of call, comparison, etc. We are not going to consider all of them now because you should just understand what parts the source code is made up of and how to read it correctly.

Interface

When we use a coffee machine or drive a car, there is a set of controls to interact with. In programming, this is called an interface.

Interface is a set of properties and methods of an entity available for use in the source code.

Property

Everyone has properties, e.g. height, weight, eye color, that is, some descriptive characteristics. Likewise, data has properties, for example, a string has a length. The syntax for accessing a property is very simple: use a dot.

entity.property_name

For clarity, let's access the length property of a string; it contains the number of characters in the string.

"JavaScript is awesome".length;

Method

This is an action call, e.g. sit down or swim, that is, some kind of activity. Data has its own predefined methods, e.g. you can add or remove elements from a collection, convert a string to different cases, etc. The syntax for calling a method is very similar to accessing a property, but parentheses are added at the end.

entity.method_name()

For example, let's look at the toUpperCase() method, which capitalizes all letters.

"JavaScript is awesome".toUpperCase();

Strict mode

A new feature in the ECMAScript 5 specification makes it possible to ensure the compliance of a script with the current standard. This prevents certain errors such as using unsafe and non-current constructs.

In order to run your script in strict mode, it is enough to enter the following directive at the beginning of the js-file. Always write your code in strict mode.

script.js
"use strict";
// This is a comment. Next comes all the code of the JS file