Variables are related to numeric values, which store the value, and can store operational data with variables.
JS differs from other languages in that it is non-type. A variable can hold any type of value, while other languages need to hold a value of a particular type.
var i=5;
I= "Fdsfad";
This is legal.
The declaration of a variable is typically declared by the Var keyword.
var i,sum; Declare two variables at a time, if the variable does not have a given initial value, the value is "undefined"
In JS, it is not wrong to declare the same variable multiple times, just the nature of assigning the variable.
Also, if you declare a variable without the var keyword, JS implicitly declares the variable and is a global variable.
Scope of the variable:
It is divided into global variables and local variables.
Global variables are in the entire JS code, everywhere has its definition, can be used everywhere.
Local variables, usually defined in a function body, can only be used locally in the function body, external is not available, and parameters are also local variables.
See here, we pay more attention to the declaration of the variable, if the variable is declared inside the function, without the keyword Var, it is a global variable, if the variable name is the same as the external variable name, then modify the value in it
vars ="sssss"; function Check () {//var s = "dddddd"; //plus Var is the effect of local variables on external ss ="dddddd";//no VAR is a global variable that has an effect on the external global variable sdocument.write (S +"<br>"); F="fffffffff"; global variable document.write (f+"<br>"); } check (); document.write (S+"<br>"); document.write (f); output: Ddddddfffffffffddddddfffffffff
These are the uses of the Var declaration that we want to pay attention to.
There is no block-level definition field in JS.
That is, all the variables declared in the function, regardless of where they are declared, are defined throughout the function.
function Test (a) {vari =0; if(A = =0) { varj =0; for(varK =0; K <Ten; k++) {document.write (k); //0-9} document.write (k); //Ten} document.write (j); //0} Test (0);
varSCOP ="alls"; function Test1 () {alert (SCOP); //output undefined varSCOP ="Local"; alert (SCOP); //output Local} test1 (); Since local variables are defined within the entire function body, it means that global variables with the same name are hidden in the entire function body, in fact, as in the following function. varSCOP ="alls"; function Test2 () {varSCOP; alert (SCOP); //output undefinedSCOP ="Local"; alert (SCOP); //output Local} test2 ();
This we want to understand the scope of variables, as well as know that there is no block-level scope in the JS function