Variables are used to store information .
A variable consists of a name and a dedicated area of memory that corresponds to it.
To declare or, in other words, create a variable, use the keyword var:
After the declaration, you can write to the variable data:
message = ‘Hello’; // save variable string
This data will be stored in the corresponding memory area and will be available later on by name:
message = ‘Hello!’;
alert( message ); // displays the contents of the variable
For brevity, you can combine variable declaration and data entry:
var message = ‘Hello!’;
You can even declare several variables at once:
var user = ‘John’, age = 25, message = ‘Hello’;
Analogy from life
The easiest way to understand a variable is to present it as a “box” for data, with a unique name.
For example, a variable message is a box in which the value is stored “Hello!”:
You can put any value in the box, and later change it. The value in a variable can be changed as many times as necessary:
message = ‘Hello!’;
message = ‘World!’; // replaced value
alert( message );
When the value changes, the old content of the variable is deleted.
You can declare two variables and copy data from one to another:
var hello = ‘Hello world!’;
// copied value
message = hello;
alert( hello ); // Hello world!
alert( message ); // Hello world!
On a note:
In such languages, once put the value in the box – and it is stored there forever, neither deleted nor changed. But you need to save something else – if you please, create a new box (declare a new variable), reuse is impossible.
In appearance, it is not very convenient, but, oddly enough, it is quite possible to successfully program in such languages. Moreover, it turns out that in a number of areas, for example in parallelizing calculations, they have advantages. The study of some functional language is recommended to expand the horizons.
- The name may consist of: letters, numbers, symbols $and_
- The first character must not be a number.
What is particularly interesting is that the dollar ‘$’and underscore characters ‘_’are just as common as the letters:
var $ = 1; // declared a variable named ‘$’
var _ = 2; // variable named ‘_’
alert( $ + _ ); // 3
And such variables would be wrong:
var 1a; // the beginning can not be a figure
var my-name; // hyphen ‘-‘ is not a valid character
Case of letters matters
Variables apple and AppLE- two different variables.
There is a list of reserved words that cannot be used for variables, as they are used by the language itself, for example: var, class, return, export etc.
For example, this example will produce a syntax error:
var return = 5; // mistake
Importance of the var directive
num = 5; // the num variable will be created if it was not
In the mode, “use strict”this is no longer possible.
The following code will generate an error:
num = 5; // error: num is not defined
Note that the directive use strict must be placed before the code, otherwise it will not work:
“use strict”; // too late
num = 5; // there will be no error since strict mode is not activated
Error in IE8- without var
If you are going to support IE8-, then I have another reason for you to always use var.
The following document in IE8 will not output anything, there will be an error:
test = 5; // there will be a mistake here!
alert( test ); // will not work
This is because the variable is testnot declared through varand matches the id element <div>. Don’t even ask why – this is a bug in IE browser before version 9.
The most “funny” is that such an error in assigning values will be only in IE8 – and only if there is an element with on the page id that matches the variable name.
Such errors are especially “fun” to correct and debug.
The conclusion is simple – always declare the variables through var, and there will be no surprises. Even in old IE.
A constant is a variable that never changes. As a rule, they are called in capital letters, underlined.
var COLOR_RED = “#F00”;
var COLOR_GREEN = “#0F0”;
var COLOR_BLUE = “#00F”;
var COLOR_ORANGE = “#FF7F00”;
var color = COLOR_ORANGE;
alert( color ); // #FF7F00
Technically, a constant is a regular variable, that is, it can be changed. But we agree not to do this.
Why do we need constants? Why not just write var color = “#FF7F00”?
- First, the constant COLOR_ORANGEis a friendly name. By assigning var color=”#FF7F00″it is not clear that the color is orange. In other words, the constant COLOR_ORANGEis an “understandable pseudonym” for the value #FF7F00.
- Secondly, a typo in the string, especially as complex as #FF7F00it may be, is not seen, and in the name of a constant it is much more difficult to admit it.
Constants are used instead of lines and numbers to make the program clearer and to avoid errors.
- Technically, you can simply write the value without declaring a variable, but for a number of reasons it is not recommended.
- At the ad can immediately assign the value: var x = 10.
- Variables that are named БОЛЬШИМИ_БУКВАМИare constants, that is, they never change. As a rule, they are used for convenience so that there are fewer errors.
- Declare two variables: adminand name.
- Copy value from name to admin.
- Output admin(should output “Vasily”).