JavaScript constants and variables, expressions-basic syntax (3)

JavaScript basic syntax (3)

 Constants and variables

Programming is essentially processing all kinds of data.

In the process of processing data, a container needs to be used to save the data, so that the data in the container can be processed.

According to whether the data stored in the container can be changed during the process of the program , the container is divided into two types: constant and variable

1. Constant

A constant is a constant value or data item. Constants in JS are also called literal constants.

Constants in JS can be divided into the following types: After the ES6 version, the definition statement of constants was introduced :: const, constants can be divided into:

  • Integer constant: an integer that cannot be changed.
  • Real constant: It is a real or floating-point constant, that is, a constant decimal. Such as: 12.32, 192.38, or 5e7.
  • Boolean constants: two kinds of true and false.
  • String type constant: it is a string value.
  • Null value: nothing.

 

Second, the variables

Variables in JS are used to store the value of the script, so that where you need to use this value, you can use variables to represent. The value in the variable can be all types of data values.

Variables in JS have less strict requirements on variable types. When we declare JS variables, we do n’t have to declare the type strictly for each variable.

Just use a var:

var a = 100; // a is an integer type 
var b = "abc"; // b is a string type 
var c = true ; // c is a boolean type

Not even var:

a = 100; // a is an integer type 
b = "abc"; // b is a string type 
c = true ; // c is a boolean type

In terms of rules, variables in JS can be used directly without being declared in advance, but we do not recommend this. We must develop good programming habits that are declared first and then used!

The variables in JS are the same as in Java, and they are divided into local variables and global variables:

  • Local variables are the variables defined in the function, and the scope is in the function body;
  • Global variables are variables defined outside the function and are valid throughout the file.

Note: If you declare a variable, do not write var, whether your variable is inside or outside the function body, the default is a global variable.

example:

<script type="text/javascript">
    var a1="北冥";//全局变量
    show1();
    function show1(){
        var a2="最帅";//局部变量
        a3 = "666";//全局变量
        document.write("the a1 is "+a1+"<br>");
        document.write("the a2 is "+a2+"<br>");
    }
        document.write (a1);
        document.write (a2);//报错
        document.write (a3);
</script>

When running this code, an error occurs, undefined, because a2 is defined as a local variable in the function show1, and after the function show1, a2 is released.

Note: In some cases, the use of global variables and local variables at the same time will cause logic errors, so for variables to be used in multiple places in the program must be defined as global variables, and it is best to use var name to avoid confusion!

Note: When taking the variable name, the basic naming rules should be observed: the variable name is a combination of upper and lower case English, numbers, $ and _, and cannot start with a number. Variable names cannot be JavaScript keywords (reserved words).

 

expression

An expression is a "phrase" in JS, and JS's interpreter can calculate it to generate a value. The simplest expression is a constant or variable.

5.20              // A numeric constant 
"beiming"      // A string constant 
true              // A Boolean constant 
null              // Null constant 
(2,3,3,3,5,3)     // An array constant 
i                 // Variable i 
sun               // variable sun
    

 

The value of a constant expression is itself, and the value of a variable expression is the value stored or referenced in the variable. The above expression is the simplest expression. We can also combine it to make a simple expression into a complex expression: i + 5.20.

As you can see, simple expressions become more complex in nature, linked and combined by operators.

 

Guess you like

Origin www.cnblogs.com/beimingdaoren/p/12757812.html