In this tutorial you will learn the basics of programming, which are required to write a script. These notions are similar to those of PHP and other programming languages.
1. Using variables
In a script (or program) we use constants and variables datas. The variables can change their values during program execution. These data are called "variables".
The simplest way to use and refer to a variable is to write it. The name of the variable permits the access at its value and also can change the value if necessary.
- Variable = name of a location in computer memory, used to store data.
You can create a variable and assign a value to it in this way:
- With statement var
var name = value;
- You can change its value just using the variable name (without "var"):
name = other_value;
var x = "xyz";
x = 8;
- Notice that the 'string' values (consisting of letters) are written between quotation marks (simples or doubles), and the 'number' can be written without quotation marks.
The variables created out of the functions can be used everywhere in that script, inclusively inside the functions and other JS script from that HTML page.
To work with datas in a script and manipulate variable values we use operators.
Operators are symbols and identifiers that determines how data are modified and how it is evaluated a combination of expressions and variables.
Operators are several types:
- - binary operators - that need two operands.
- - unary operators - that need only one operand.
These will be detailed in the next lesson.
- Arithmetic operators
- Assigning operators
- Comparation operators
- Logical operators (booleans)
- Operators for strings
- Typeof operator
- Conditional operator (? :)
- Operators for functions
- Operators for objects