JavaScript literals and variables

JavaScript literal

Literal value: a fixed value, which means what the literal means

  • Number literal : It
    can be an integer or decimal, or scientific notation.
3.14
1001
123e5
  • String literal :
    You can use single or double quotes.
"John Doe"
"John Doe"
  • Boolean literal :
    There are only two values, which are true and false.

  • Expression literal :
    used for calculation, use operators to perform calculations, such as arithmetic, bit, condition, comparison, and logical operators.

5 + 6
5 * 10
  • Array literal :
    define an array
[40, 100, 1, 5, 25, 10]
  • Object literal :
    define an object
{
firstName:"John", 
lastName:"Doe",
age:50, 
eyeColor:"blue"
}
  • Function literal : define a function
function myFunction(a, b) {
 return a * b;
}

JavaScript variables

Variables are containers for storing data, which can change or be assigned again during the running of the program.

Declare variable

In JavaScript, declare variables using varstatements.

[Example 1]
In a var statement, you can declare one or more variables, or assign values ​​to variables. Unassigned variables are initialized to undefined (undefined) values. When declaring multiple variables, they should be separated by the comma operator.

var a;  //声明一个变量
var a,b,c;  //声明多个变量
var b = 1; //声明并赋值
document.write(a);  //返回 undefined
document.write(b);  //返回 1

[Example 2]
In JavaScript, the same variable can be declared repeatedly, or the value of the variable can be initialized repeatedly.

1.  var a = 1;
2.  var a = 2;
3.  var a = 3;
4.  document.write(a);  //返回 3

Note:
In non-strict mode, JavaScript allows variables to be assigned directly without declaring them. This is because the JavaScript interpreter can automatically declare variables implicitly. Implicitly declared variables are always used as global variables. In strict mode, variables must be declared before they can be used.

Assignment Variables
Use the equal sign =operator to assign values ​​to variables. The left side of the equal sign is the variable, and the right side is the assigned value.

[Example]
Variable promotion. JavaScript will pre-process the declared variables in the pre-compilation period, but the assignment of variables occurs during the JavaScript execution period, not the pre-compilation period.

1.  document.write(a); //显示undefined
2.  a =1;
3.  document.write(a); //显示 1
4.  var a;

The variable declaration is placed at the end, and the assignment operation is placed at the front. Since JavaScript has pre-parsed the variable declaration statement during the pre-compilation period, the first line of code will not throw an exception when reading the variable value, but will return the uninitialized value undefined. The third line of code is read after the assignment operation, so it is displayed as the number 1.

Tip:
The way to parse the JavaScript engine is to parse the code first, get all the declared variables, and then run it line by line. In this way, all declared variables will be promoted to the head of the code, which is called variable promotion (Hoisting).

Variable Scope
Variable scope (Scope) refers to the effective range of variables that can be accessed in the program, also known as the visibility of variables.

JavaScript variables can be divided into global variables and local variables:

  • Global variables: Variables are visible in the entire page script and can be freely accessed.
  • Local variables: Variables can only be visible inside the declared function, and access outside the function is not allowed.

[Example 1]
The relationship between global variables and local variables

1.  var a = 1;  //声明并初始化全局变量
2.  function f(){  //声明函数
3.  document.write(a);  //显示undefined
4.  var a = 2;  //声明并初始化局部变量
5.  document.write(a);  //显示 2
6.  }
7.  f(); //调用函数

Since a local variable a with the same name is declared inside the function, JavaScript uses this variable to override the influence of global variables inside the function during the pre-compilation period. In the early stage of execution, the local variable a is not assigned, so the value of the local variable a is undefined when the first line of code in the function reads the value of the local variable a. When the execution reaches the second line of the function, the local variable is assigned a value of 2, so it is displayed as 2 in the third line.

[Example 2]
Demonstrate the consequences of not explicitly declaring local variables

1.  var jQuery = 1;
2.  (function () {
3.  jQuery = window.jQuery = window.$ = function(){};
4.  })()
5.  document.write(jQuery);  //显示函数代码:function(){}

Therefore, using global variables in the function body is a dangerous behavior. In order to avoid such problems, you should develop the habit of using var statements to explicitly declare local variables in the function body.

Variable type
JavaScript is a weakly typed language, and the specification for variable types is relatively loose. The specific performance is as follows:

  • The type classification of variables is not rigorous and unclear, which brings arbitrariness to use.
  • When declaring a variable, it is not required to specify the type.
  • The use process is not strict, and variable types can be automatically converted as needed.
  • There is no uniform and standardized method for variable conversion and type checking, which leads to low development efficiency.

Advantages and disadvantages:

  • Advantages: flexible to use, simplifies code writing.
  • Disadvantages: The execution efficiency is low, and the performance of the program will be affected when large-scale applications are developed.

Guess you like

Origin blog.csdn.net/QIANDXX/article/details/113746305