JavaScript variables

 Variables in JavaScript have similar meanings to variables in many programming languages. You can imagine variables as an empty box that can be used to hold things. Variable declarations in JavaScript have certain rules. The first letter of a variable must be an English letter. , Can be followed by underscore "_" or numbers, such as A_01, BBB, C02... etc. Note that when declaring variables, the case must be set first, because JavaScript variables are case-sensitive, such as A_01 It is a different variable from a_01! In addition, the use of Chinese is not currently supported.


JavaScript variable declaration example
<script type="text/JavaScript"><!--
 var a;
 a='10';
 var b='20';
 c='30';
--></script>

JavaScript can declare a variable in two ways. In the example, a variable a is declared through var. The second line a='10' means that the variable value is set to the integer 10. We can also use the equal sign to give the variable value in the declaration place. Like var b='20' in the third line, a more simplified way of writing is to look at c='30' in the fourth line, which means you don't even need to declare. There is no special restriction on the length of variable names, but too long values ​​should not be used to avoid unmanageable when there are more and more codes.

Types of JavaScript variables

JavaScript will automatically process the types of variables. After declaring a variable and assigning a value, JavaScript will judge by itself.
<script type="text/JavaScript"><!--
 var abc;
 abc=100; // Integer type
 abc="Test Text"; // String type
 abc=true; // Boolean value type
 abc=new Array() // Array type
--></script>

JavaScript Global Variables and Local Variables
<script type="text/JavaScript"><!--

 var A01='100'; //Global Variables

 function Test(){
  var A02='200'; //Local Variables
 }

--></script>
The first variable A01 is a global variable (Global Variables), that is, it can be used everywhere in the file, and A02 in the function is a local variable (Local Variables), which can only be used in the function, sometimes it can Set global variables first, then make changes in the function, and declare it once so that each function can be called. This way of declaration is often seen in some relatively large systems.

Post a Comment

0 Comments