Program Structure in JavaScript

Tram Ho

Program Structure in JavaScript

In previous lessons, I introduced you to the basic components of JavaScript (values, types, and operators). But these are just the raw materials. So how to combine these components into a complete program. To solve this problem, we will learn about program structure in JavaScript.


Therefore, each directly written value (66, ‘hello’, true, NaN,…) is an expression. Or combining values with operators (1 + 5, ‘I’ + ‘ love’ + ‘ you’, !false, …) are also expressions.

Expressions can be simple additions, subtractions,… Or they can be complex mathematical, physical, … formulas. And in an expression can contain many subexpressions.


Statements are syntactic structures and commands that perform actions.

We have seen a statement, alert(‘Hello, world!’) which shows the message “Hello, world!”.

We can have as many statements in our code as we like. Statements can be separated by semicolons.

For example, here we split “Hello World” into two alerts:

Usually, statements are written on separate lines to make the code more readable:



In most programming languages, a statement is usually terminated by a semicolon. So does JavaScript. Additionally, JavaScript allows you to omit semicolons. Then, each statement will be on one line.


Keywords are words with special meanings. The word var is a keyword. The var keyword following it will be a variable (var stands for variable).

Reserved words in JavaScript

Reserved words are words that will be used as keywords for later versions of JavaScript. You will not be able to use keywords or reserve words to name variables.

=> Here is a list of keywords and self-reservations:

break case catch class const continue debugger

default delete do else enum export extends false

finally for function if implements import in

instanceof interface let new null package private

protected public return static super switch this

throw true try typeof var void while with yield

Variables in JavaScript

Variables are used to store values, which we can use in other programs.

How to use variables: Var <Tenbien> = <expression>;


In the above example, I will use the keyword var and after the expressions I use the assignment operator (=) and finally and the expression.

After declaring as above, we can use the variable to actually use the variable.

Also, after you assign a value to a variable, you can still assign that variable to a different value.

How to name variables

JavaScript has the following rules for naming variables:

Starts with a letter, underscore (_) or the character “dollar” ($)

After the first character, in addition to the above characters, you can use more numbers (number)

Do not use keywords and reserve words

Examples of valid names:

Examples of invalid names

Standardize variable naming

The problem I want to mention here is how to name it so that when you look at it, you immediately know its meaning. To solve this problem, there are two schools of naming as follows:

Use underscores to separate words: dien_tich_ao

Using Camel Case (you simply capitalize the first letter of each word, minus the first): dienTichAo

In my experience, I will use a combination of the two methods above. Usually I will use the second method (Camel Case). However, when I want to use a variable as a constant, I will use method one. For now, I will capitalize all characters.

Standard naming helps your code stay consistent. Especially, in a project with many programmers involved, imagine if each person named it in a different style? God, I wouldn’t want to read that crappy code!.

Share the news now