[Rough version] JavaScript variables, data types, operators, process structure


Contents of this article:

  • javascript variables
  • javascript data type
  • javascript operators
  • javascript process structure

 

First release date: 2018-05-09


javascript variables

 

Create variable:

Global variables: Global variables are variables that can be called anywhere in the js code

  • [When not within {}] var variable name = value;
  • [Whether in {} or not] variable name = value;

Local variables: Local variables are variables that only take effect within the body of the function

  • [In {}] var variable name = value;
  • [The parameter variable of a function is also a local variable]

image

 

Scope:

  • In the function body, variables with the same name, local variables have higher priority than global variables
  • Local variables cannot be called outside the function;
  • In nested functions, the outer function cannot call the local variables of the inner function, and the memory function can call the local variables of the outer function

 

 

Replenish:

  • The scope of variables is a big hole, and there are scope chain problems and life cycle problems [for example, the priority of a function name with the same name as a variable is also an unusual problem]. Use with caution. Since this is a simple record, so not record so complicated things.


javascript data type

 

  • JavaScript is a dynamically typed language, it uses var to declare each object uniformly, and the data of each object determines what kind of data type it is.
  • So the following mainly introduces the relationship between data types and variables

 

 

 

Numeric:

  • number: an ordinary number, which can be represented by octal, ten, hexadecimal, etc., and can be a floating-point number.

 

 

Character type:

  • string: characters wrapped by "" or ''

 

Boolean:

  • boolean: There are two values, one is true and the other is false;

 

Reference type:

  • undefined: A variable is declared but not initialized by assignment.
  • object: indicates that the variable is an object; when the variable is assigned null, it represents an empty object.

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <script>
        function f1(){
            var test=6;//Number
            var a="7";//String
            var b=true;
            var c;
            var d=null;
            var e={'name':"lilei"};
            console.log(typeof(test));//number
            console.log(typeof(a));//string
            console.log(typeof(b));//boolean
            console.log(typeof(c));//undefined
            console.log(typeof(d));//object
            console.log(typeof(e));//object
        }
    </script>
</head>
<body>
    <input type="button" value="点击" onclick="f1()">
</body>
</html>
image

 

Replenish:

  • Escaping of strings: Some characters in strings can have specific meanings, such as \n which will become line breaks when displayed on a web page. If you want to "do not use that special meaning", you need to add \ in front of these special characters
  • typeof(variable name) can return the data type of the variable.

 

 


javascript operators

 

Arithmetic operators:

  • + addition operator
    • In addition to the function of adding numbers, the addition operator can also concatenate strings. For example, the result of "a"+"b" is "ab", and the result of 7+"a" is "7a".
  • - Subtraction operator
  • *Multiplication operator
  • / division operator
  • % remainder operator
  • += addition assignment operator, -= subtraction assignment operator, *= multiplication assignment operator, /= division assignment operator, %= remainder assignment operator

 

Increment and decrement operators:

  • ++: Self-increment operator, which adds one to its own value
  • --: Decrement operator, which subtracts one from its own value
  • The auto-increment and auto-decrement operators can be located before the variable [auto-increment first, then take the value], or it can be located after the variable [first take the value, then auto-increment],

image

 

Relational operators:

  • <= : Not greater than operator, such as a <= b, if a is not greater than b, then return true, otherwise return false
  • < : less than operator
  • > : greater than operator
  • >= : not less than operator
  • == : equals operator
  • !== : not equal operator
  • Comparison rules:
    • Between values: directly compare the size of the values
    • One is a number: convert the other to a number
    • Between strings: compare the size of unicode values ​​between them
    • String and non-numeric: convert non-numeric to string
    • other. . .

 

Logical Operators:

  • ! : Logical NOT operation, invert the result, the returned result is a Boolean value,
  • && : logical AND operation, the return result is a boolean value,
  • || : logical OR operation, the return result is a boolean value

 

 

Bitwise operators:

  • ~: bitwise NOT
  • & bitwise AND
  • |Bitwise OR
  • ^ bitwise XOR
  • << bit left shift operation
  • >> signed right shift operation
  • >>> unsigned right shift operation

 

Other operators:

  • ?: : This is a ternary operator, the expression (expr1) ? (expr2) : (expr3). The value is expr2 when expr1 evaluates to TRUE and the value expr3 when expr1 evaluates to FALSE.image

 


javascript process structure

 

Loop structure:

  • while structure
    • while(conditional expression){loop executes the code segment}
    • image
  • do-while structure
    • do{loop execution code segment}while (conditional expression);
    • image
    • The do-while loop executes once regardless of whether the initial condition is true or not
  • for structure
    • for (loop variable = initial value; loop condition; counter) {loop execution code segment}
    • image
  • for-each construct
    • for (loop variable in set) {loop execution code segment}
    • image

 

 

choose structure

  • if
    • if(conditional expression 1){code to be executed when conditional expression 1 is true}else if(conditional expression 2){code to be executed when conditional expression 2 is true}else{code to be executed when none of the previous conditions are met }
    • image
    • else if and else are optional
  • switch
    • switch(value){ case value 1: code segment; break; case value 2: code segment; break; case value 3: code segment; break;...default: code segment;}
    • image

 

Closing statement:

  • return: End the execution of the function body where return is located. and returns the value followed by return.
  • break: Ends the loop where break is located. break can also be used in switch to jump out of selection, without break it will execute another case downwards.
  • continue: End the loop in advance at the position of continue, and then execute the next loop judgment.

 

 

 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326395194&siteId=291194637