JavaScript study notes (2): js basic syntax

0 Table of contents

  • variable
  • Value type (data type)
  • Arithmetic operator
  • Comparison operators and logical operators
  • Conditional statements
  • Initial reference value
  • Basic rules of js statement

1 Variable

1.1
Declaration of Variables Use the var keyword to declare variables.
Usages are as follows:var a = 100;

1.2 Variable naming rules

  • Variable names can contain English letters, underscores, $, numbers
  • Variable names must start with one of English letters, underscore and $
  • Do not use system keywords and reserved words as variable names, such as: int, float, var, etc.

2 Value type (data type)

2.1
Five types of unchangeable original value (stack data) : Number, String, Boolean, undefined (unassigned), null (null value)

2.2 Reference value (heap data)
such as: array array, object, method function, etc.

2.3 The process and difference between the declaration and assignment of original and reference values

  • Original value declaration and assignment process:

  • Reference value declaration and assignment process:
    such as a statement var arr = [1, 2];, first apply for a memory space in the stack area, named arr, and then put the data [1, 2] into a space in the heap area, and then use the space address of the heap area as The value is placed in the stack space arr.

Statement var arr1 = arr;, apply for a memory space in the stack area, named arr1, and then copy the address in the stack area space arr to the stack area space arr1, the heap area space for storing data [1, 2] does not change.

Re-assign the variable arr:, the arr = [1, 2, 3]previous two operations, so that the addresses stored in the stack space arr and stack space arr1, respectively, point to the heap space 1010, the original data stored in the heap space 1010 is [1, 2], for the variable arr After reassignment, the data stored in the heap space becomes [1, 2, 3], which leads to after reassignment 变量arr的值 = 变量arr1的值 = [1, 2, 3].

3 Arithmetic operators

3.1 Plus sign: "+"

  • The plus sign can be used for mathematical operations, for example var a = 1; a = a+1;, the result a is equal to 2
  • The plus sign can also be used for string concatenation, for example var a = 'aaa'; var b = 'bbb'; var c = a+b;, the result c is equal to 'aaabbb'
  • Any data type performs a "+" operation with a string, and the resulting data type is a string type. For example var a = 1; var b = 'b'; var c = a+b;, the result c is equal to '1b' and the data type is string

3.2 Other operators

- * / % = ( )
Subtraction Multiplication Division Divide by Assignment Brackets change priority

Among them, "=" has the lowest priority, bracket "()" has the highest priority

3.3 Special operators
"++", "-", "+ =", "-=", "* =", "/ =", "% ="
usage is consistent with usage in other high-level languages, no longer here Give an example.

4 Comparison and logical operators

4.1 Comparison operators
"<", ">", "==", "> =", "<=", "! ="
Are respectively less than, greater than, equal to, greater than or equal to, less than or equal to, not equal to

4.2 Logical operators

&& || !
versus or non-

5 Conditional statements

5.1 if statement

if(表达式){
    语句;
}

5.2 if-else statement

if(表达式){
    语句;
} else {
    语句;
}

5.3 else if statement

if(表达式1){
    语句1;
}else if(表达式2){
    语句2;
} else {
    语句3;
}

5.4 for loop

for(初始化表达式; 条件表达式; 更新表达式){
    语句;
}

5.5 while和do while

while(循环条件){
    循环体;
}
do{
    循环体
}while(循环条件);

The most obvious difference between while and do-while is that do-while executes a loop at the beginning, so the do-while statement will execute the loop at least once
5.6 switch-case

switch(表达式){
    case n:
        语句...
    case n:
        语句...
    default:
        语句...

It should be noted that the case statement only identifies the starting point of the execution of the switch statement. Once the conditional statement of the case is met, all subsequent cases will be executed until the end. So generally add break in the case as the end of the statement:

switch(表达式) {
    case n:
        代码块
        break;
    case n:
        代码块
        break;
    default:
        默认代码块;
} 

5.7 break and continue
In the loop process, use the break statement to directly end the loop, thereby executing the statement behind the loop,
and use the continue statement to end the current cycle and continue to execute the next cycle

6 Initial reference value

6.1 Array
The declaration method of the array is as follows: var arr = [1, 2, 3, 4, 5, "abc", true]
fetching data from the array is by subscript, such as:, var a = arr[0]a value is 1;, var b = arr[1]b value is 2
takes the length of the array, for example var c = arr.length, c value is 7

6.2 Objects
Objects include attribute values ​​and attribute names. They are all enclosed by "{}".
Object declaration methods are as follows:

var obj = {
    name: "xiaowu",
    age: 22,
    sex: "男"
}

Among them, name, age, sex are attribute names; "xiaowu", 22, "male" are attribute values

The way to get the attribute value from the object, such as:, var a = obj.namethe value of a is the string "xiaowu"

7 Basic rules of js statement

The 7.1 statement must be followed by a semicolon. For example, the var a = 1;
writing format should be standardized: "=, +, /,-" should be added to both sides of these operators, such as: var a = b + 1;
7.3 JS syntax error will cause subsequent code to terminate execution, But it will not affect other js code blocks. For example, there are multiple script tags in an HTML file, and a script error occurs in one script tag and terminates, but this situation will not affect the execution of js code in another script tag. . Examples:

<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <script>
            var a = 10:	//冒号不能作为语句的结尾,所以语法解析错误,会报错
        </script>

        <script>
            var b = 20;	//上面的script标签报错并不会影响这个标签里的代码执行,这行代码将会继续正常执行
        </script>
    </body>
</html>

7.4 There are two kinds of errors in js statements:

  • Syntax parsing error (low-level error)
    Before executing the js code, the browser will check all the code. If there is a syntax parsing error, the error will be reported directly, and the sentence will not be executed, such as :
    var a = 10+, a plus sign cannot be used as the end of the statement , So there is a grammatical parsing error and an error is reported directly.

  • Examples of logical errors (other than syntax errors) :
var a = 10;
document.write(a);
document.write(b);
document.write("20");

There is only one "10" in the running result of the code, because in the third line, b is not declared, it is a logic error. When a logic error is encountered, the code before the error is executed normally, and the execution will stop immediately when the error is reached. , And the code behind the error will not be executed.

Guess you like

Origin www.cnblogs.com/xiaowus/p/12695107.html