You get a bonus - 1 coin for daily activity. Now you have 1 coin

Javascript variables

Lecture




  1. Variable
    1. Analogy from life
    2. Copying values
  2. Importance of the var directive
  3. Constants

Depending on what you are doing the script, you will need to work with the information.

If this is an electronic store, then this is a goods basket. If the chat - visitors, messages and so on.

Variables are used to store information.

Variable

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 var keyword:

var message;

After the declaration, you can write to the variable data:

var message;
message = 'Привет' ; // сохраним в переменной строку

This data will be stored in the corresponding memory area and will be available later on by name:

1 var message;
2 message = 'Привет' ;
3
4 alert(message); // выведет содержимое переменной

For brevity, you can combine a variable declaration and a data record:

var message = 'Привет' ;

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, the message variable is a box that stores the value of "Привет" :

  Javascript variables

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:

1 var message;
2
3 message = 'Привет' ;
4
5 message = 'Мир' ; // заменили значение
6
7 alert(message);

  Javascript variables

When the value changes, the old contents of the variable are deleted.

There are functional programming languages ​​in which the value of a variable cannot be changed.

In such languages, once put the value in the box - and it is stored there forever, neither deleted nor changed. And 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. The study of some functional language is recommended to expand the horizons. An excellent candidate for this is the Erlang language.

Copying values

Variables in JavaScript can store not only strings, but also other data, such as numbers.

Let's declare two variables, put a string in one, and a number in the other.
As you can see, the variable does not matter what to store:

var num = 100500;
var message = 'Привет' ;

The value can be copied from one variable to another.

1 var num = 100500;
2 var message = 'Привет' ;
3
4 message = num;
The value from num overwrites the current in message .

In the "box" message value changes :

  Javascript variables

After this assignment, in both num and message boxes is the same value 100500 .

Importance of the var directive

In JavaScript, you can create a variable without var , just assign a value to it:

x = "value" ; // переменная создана, если ее не было

Technically, this will not cause an error, but you shouldn’t do it anyway.

Always define variables through var . This is a good programming tone and helps to avoid mistakes.

Open an example in IE in a new window :

01 < html >
02 < head >
03    < meta http-equiv = "X-UA-Compatible" content = "IE=8" >
04 </ head >
05 < body >
06    < div id = "test" ></ div >
07
08    <script>
09      test = 5;
10      alert(test);
11    </script>
12
13 </ body >
14 </ html >
Open code in new window

The value does not appear, there will be an error. If debugging is enabled in IE or the development panel is open, you will see it.

The fact is that almost all modern browsers create variables for elements that have an id .

Variable test in them in any case exists, run, for example:

1 < div id = "test" ></ div >
2
3 <script>
4    alert(test); // выведет элемент
5 </script>

.. But in IE <9 such a variable cannot be changed.

All will be well if to declare test , using var :
The correct code is:

01 < html >
02 < body >
03    < div id = "test" ></ div >
04
05    <script>
06      var test = 5;
07      alert(test);
08    </script>
09
10 </ body >
11 </ html >

The most "funny" is that this error will only be in IE <9, and only if there is an element with a matching id on the page.

Such errors are especially fun to fix and debug.

There are also situations where the absence of a var can lead to errors. I hope you are convinced of the need to always put var .

Importance: 2

  1. Declare two variables: admin and name .
  2. Write in the name line "Василий" .
  3. Copy the value from name to admin .
  4. Output admin (should output "Vasily").
Decision

Each line of the solution corresponds to one step of the task:

1 var admin, name; // две переменных через запятую
2
3 name = "Василий" ;
4
5 admin = name;
6
7 alert(admin); // "Василий"

[Open task in new window]

Constants

A constant is a variable that never changes. As a rule, they are called in capital letters, underlined. For example:

1 var COLOR_RED = "#F00" ;
2 var COLOR_GREEN = "#0F0" ;
3 var COLOR_BLUE = "#00F" ;
4 var COLOR_ORANGE = "#FF7F00" ;
5
6 alert(COLOR_RED); // #F00

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 use "#F00" or "#0F0" ?

  1. First, the constant is a friendly name, unlike the string "#FF7F00" .
  2. Secondly, a typo in the string may not be seen, and in the name of the constant it is impossible to miss it - there will be an error during execution.

Constants are used instead of lines and numbers to make the program clearer and to avoid errors.

created: 2014-10-07
updated: 2024-11-14
278



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Scripting client side JavaScript, jqvery, BackBone

Terms: Scripting client side JavaScript, jqvery, BackBone