The program is executed sequentially, from top to bottom, command after command.
The code inside the comments is not executed. Examples of comments:
// This line of code will not be executed. Single-line comment. /* These lines of code will not be executed. This is a multi-line comment. */
console.log() command can be used anywhere in the program. It can return numbers, strings (they must be expressed in quotes), results of some operations.
A variable is a name for data that is understandable to people.
You can create a variable with the
var command followed by the name of the variable. You can write the name using the
var myNumber; var userName;
var my_number; var my_name;
myName are two different variables.
The variable name must begin with a Latin letter and can only contain Latin letters and numbers.
You cannot use special keywords such as
if as the name of the variable. Here is the Full List of these keywords.
var command is used only once to create each variable. We then access the variable by its name, without
To assign a value to the variable, use the equal sign
After the variable is created, it can be used in other commands, for example, it can be logged in the console.
If a new value is assigned to the declared variable with a value, it will override the old value. This is called overriding the value of a variable.
var milkInGrams = 20; console.log(milkInGrams); // Logs 20 milkInGrams = 100; console.log(milkInGrams); // Logs 100
Examples of operations: addition (+), subtraction (-), multiplication (*), division (/).
Variables can be part of the operations:
milkInGrams * 0.5;
You can use parentheses to change the priority of operations
var firstNumber = 100 + 50 / 2; var secondNumber = (100 + 50) / 2;
If a string is involved in the addition operation, the result will be presented as a string:
console.log('Milk, g: ' + 50); // Logs 'Milk, g: 50'
Addition of strings is concatenation.