js global variables. Variables

Variables and constants in JavaScript. Declaring variables and assigning values ​​to them. Variables global and local. Using constants.

Declaring Variables in JavaScript

Variable names in JavaScript can consist of letters, numbers, the $ sign, and the _ sign, and the variable name cannot start with a number. Keep in mind that JavaScript is case sensitive and a1 and A1 are different variables. Cyrillic is not recommended, although it is possible.
Variables in JavaScript are declared with the var keyword:

Var Peremennaya_1 var Peremennaya_2

Using variables in JavaScript without a declaration is not recommended. This is possible, but may lead to errors.

Assigning a value to variables

Assigning a value to declared variables in JavaScript:

Peremennaya_1 = 25 Peremennaya_2 = "The assigned text is enclosed in straight quotes"

You can assign a value to variables immediately upon declaration:

Var Peremennaya_1 = 25 var Peremennaya_2 = "We enclose the assigned text in straight quotes"

The value of a variable in JavaScript can change during program execution. When enrolling in text variable, it must be enclosed in straight quotes.

Variables local and global

If a variable is declared within a function, then it is local and will be available (have visibility) only within that function. When a function exits, local variables in JavaScript are destroyed, so you can use variables with the same name in different functions.

If a variable is declared outside of functions, then it is global and will be available (have visibility) in all functions within the page. Global variables are destroyed in JavaScript when the page is closed.

Constants in JavaScript

Constants are designed to make it easier to work with code when you have to use repeated values ​​or expressions. It is enough to set a value for the constant once and you can use it as much as you like, inserting it into the code of your programs. JavaScript doesn't have a keyword for declaring constants; regular variables are used instead of constants. To distinguish constants from variables, it is customary to denote them with capital letters, using an underscore if necessary:

Var DRUG_CHELOVEKA = "Dog"

The given example of a constant is not quite complete, since the word “Dog” is already easy to remember and insert where necessary. You can use constants in JavaScript to write and insert more complex values, such as hard-to-remember codes, character sets, long text, web addresses, addresses Email, phone numbers, different coefficients.

In JavaScript, constants can be rewritten like variables, but if you do that, then the meaning of constants is lost.

Variables serve as "containers" for storing information.

Do You Remember School Algebra?

Do you remember school algebra? x=5, y=6, z=x+y

Do you remember that a letter (like x) could be used to store a value (like 5), and that you could use the information above to calculate that the value of z is 11?

These letters are called variables, and variables can be used to store values ​​(x=5) or expressions (z=x+y).

JavaScript variables

Just like in algebra, JavaScript variables are used to store values ​​or expressions.

The variable can have a short name like x, or a more descriptive name like carname (car name).

Rules for JavaScript variable names:

  • Variable names are case sensitive (y and Y are two different variables)
  • Variable names must start with a letter or underscore

Comment: Since JavaScript is case sensitive, variable names are also case sensitive.

Example

The value of a variable can change during script execution. You can refer to a variable by its name to display or change its value.

Declaring (Creating) JavaScript Variables

Creating variables in JavaScript is more commonly referred to as "declaring" variables.

You declare JavaScript variables with a keyword var:

After executing the suggestions above, the variable x will contain the value 5 , and car name will contain the value Mercedes.

Comment: When you assign a text value to a variable, enclose it in quotation marks.

Comment: If you re-declare a variable, it will not lose its value.

JavaScript Local Variables

A variable declared inside a JavaScript function becomes LOCAL and will only be available within this function. (the variable has a local scope).

You can declare local variables with the same name in different functions because local variables are recognized in the function in which they are declared.

Local variables are destroyed when the function exits.

You'll learn more about functions in later JavaScript lessons.

JavaScript Global Variables

Variables declared outside the function become GLOBAL, and all scripts and functions on the page can access them.

Global variables are destroyed when you close the page.

If you declare a variable without using "var", the variable always becomes GLOBAL.

Assigning Values ​​to Undeclared JavaScript Variables

If you assign values ​​to variables that have not yet been declared, the variables will automatically be declared as global variables.

These suggestions:

You'll learn more about operators in the next JavaScript lesson.

Hello! Today we will talk about the scope of variables (read what a variable is). The fact is that when you create a variable in a function and its name matches the name of a variable outside the function, then here there can be various interesting situations related to the global and local scopes of the variable.

This is exactly what we will deal with in this lesson.

global variable

Global variables are all variables that you create outside of a function. And be sure to create a variable through keyword var, if this is not done, the variable will be visible everywhere in the program, and moreover, if strict mode is enabled, this will cause an error. In order to enable strict mode, it is enough to write the line “use strict” at the beginning of your script. This will tell the JavaScript interpreter to strictly follow the JavaScript standard. Here is an example using a global variable

Vara=6; //global variable function double() ( return alert(a*a); //use of global variable ) double();

In the example, a global variable a is declared, which is assigned the value 6. In the function, we can access this variable and even change its value, and this value will change everywhere.

Let's look at an example where the value of a global variable is changed in a function, and then we access the global variable outside the function and see the value that was set to it in the function itself.

Vara=6; function double() ( a = 5; //change the value of the global variable in the function return alert(a*a); ) double(a); //call the function document.write(a); //global variable value

As you can see from the example, if you change the value of a global variable in a function, it already remains with it everywhere, both in the function and outside it.

Local variable.

When you declare a variable in a function, it becomes local and can only be accessed from within the function. It is worth noting that the if/else , for, while, do...while statements do not affect the scope of variables.

So it turns out that in a function you can access a global variable, but globally outside the function you cannot access a local variable created in the body of the function. Consider an example.

Function double() ( var a =6; return alert(a*a); ) double(); document.write(a); //try to access local variable

In the example, the variable is not declared globally, but is declared in the function, that is, locally. Then we call the function and try to access the local variable. As a result, nothing happens, and in the console we see an error that the variable is not defined.

And the last option is when both a global variable and a local one are created. the same name what will happen then. Let's watch an example.

Vara=7; function double() ( var a =6; return alert(a*a); ) document.write(a);

As you can see, another variable is created in the function, despite the fact that its name is the same as the global one and it is the local variable that will be available in the function, it will not be able to overwrite the global one, which this example shows.

What conclusion can be drawn from all this. It's like trying to use variables with different names both in the function and outside the function. But you need to know about local and global scopes.

Results.

A variable created outside of a function is global.

From a function, you can access a global variable and change its value.

What are global variables: variables are "visible" at any point in the execution of the program, anywhere from can be read and overwritten.
Usually global variables are defined at the very beginning of the program, outside of any blocks (())
in the case of Js, they are set after the script, or all functions

hello = Hello ; //set global variable and check it
document. writeln (->1 + hello + every one
); //->1 Hello every one

if(true)
//if(false)
{
var hello = Hello Dolly and ; //this is also global
world = world; //global
var cont = , we continue //global
document. writeln (->1.2 + hello + world + cont +
);
//1.2 Hello Dolly and world, we continue
}
document. writeln (->2 + hello + world + cont +
);
//->2 Hello Dolly and world, we continue


if true we get the answer

->1 Hello everyone
->1.2 Hello Dolly and world, we continue
->2 Hello Dolly and world, we continue


execution breaks

Thus, it turns out that the use of var does not affect the global scope in any way. A variable mentioned in a script outside of procedures is considered global even if it is enclosed in parentheses () of an if block while for and other areas will remain global even inside the loops

Conclusions, briefly

  • for variables used in the global scope, the presence of var is not important.
  • Blocks after if while for do not create local scoping (as they do in other languages)
local variables - variables set inside the executable block (function) and do not affect other functions. and the external environment, i.e. global area.

boy = John;
did = kills Kenny ; //so we have 2 global variables
function doit()
{
//print has already passed --1, and we change the data change the data
var boy = Gary ; // create locale
did = help Annie ; // change the global
;
document. writeln (--2 + boy + + did +
);
//--2 Gary helps Annie
;
//now inside the function we set the local and global variables
var good = he was a good boy
; //locale!
bad = he likes a bad girls
; //global
}
;
document. writeln(--1+boy++did+
);
//--1 Jhone kills Kenny
doit();
//--2 Gary helps Annie
document. writeln (--3 + boy + + did +
);
//--3 Jhone helps Annie
;
if(!true)
//if(!false)
{
document. writeln (--4 + good );
//execution of this block will raise an error.
//we are now out of local scope
// doit() functions, so for us it is given via var
//variable good just doesn't exist
}
document. writeln (--5 + bad );
//--5 he likes a bad girls


Result:

1 Jhone kills Kenny
--2 Gary helps Annie
--3 Jhone helps Annie
--5 he likes a bad girls


Conclusion

local variables in javascript

  • var works inside a function by declaring a local variable. This is his main task.
Recommendations and remarks
  • Javascript is very different from C already in that only (?) Local variables are possible inside a function.
  • using var or not using it in the global scope is purely up to your personal experience. But for me it's better not to be lazy. in perl it's called use strict
>>> for variables used in the global scope, the presence of var is not important

Important. First, vars cannot be deleted (using delete). Secondly, their instantiation takes place on the “zero line” (before the work “line by line”), where they are immediately assigned the value undefined, and only then the variable may (or may not) receive a new value:

var glb_1 = 1;
if (false) (var glb_2 = 2; glb_3 = 3;)

alert(glb_1) // instantiated and set to 1
alert(glb_2) // instantiated and set to 'undefined'
alert(glb_3) // not a variable at all (no var), call error

Internet