Variables:
The meaning of a variable: It is a code used to represent a data - a name defined by ourselves.
Definition of variables: var v1; var v2 = 2; var v3, v4, v5; var v6, v7=7, v8, v9="abc";
Use of variables:
Assignment: v1 = 1; v1 = 1+2+3; v1 = v2 + “abc”; v1 = v1 + 5; v1 = v1 + v1;
Value: m1 = v1; //Get the value of v1 and assign it to m1
m2 = v1 + 5; m2 = v1 + 5;
Alert (v1); // Take the value of V1 and give it to Alert to "pop up"document.write (v1); // Receive the V1 value and hand it over to the document.write to "output"
Alert ("v1 =" + v1); // The value of V1 is obtained, and the string "v1 =" is connected to the operation, and then the result is alert.
document.write( “v1=" + v1);
Data type:
Basic type:
Boolean type: boolean; This type has only two values available: true, false
String type: string; It can be represented by single quotes or double quotes, with the same meaning;
Number type: number
Composite type:
Array: Array,
Object: Object
Special type:
NULL: empty type -can give a variable a clear assignment to "null". This is the empty value and the type of empty type, just to indicate a "meaning": the variable does not give an effective value.
undefined: Undefined: This type usually corresponds to the state of "no value has been given at all".
Arithmetic operators: + - * / %
Note:
1. Division (/) is natural division in mathematics, not the meaning of division in C language.
2, ++ and -- are called unary operators, which only operate on one variable
3, But ++ and – can be used in expressions (such as assignment statements), which is like doing two things at the same time: self-increment (or self-decrement) and the calculation of the expression itself. At this time, they are placed in The front and back of variables have different meanings.
a) var i = 1; var s1 = i++; var s1 = i++; //The result is: s1 is 1, i is 2
b) var i = 1; var s2 = ++i; //The result is: s2 is 2, i is 2
Comparison operators: > >= < <= == != === !==
Comparison operators are used to compare the size of data, usually numbers. Notable ones are:
== is called "fuzzy equality", that is, if the content of the data or the converted content is equal, it is considered equal.
=== is called "strict equality". It is considered equal only when the data type and data content are equal.
——From a computer perspective, a data has two aspects: data type and data value (data content)
Logical operators: only used to operate on bool values.
Logical AND ( && ): The result is true only when both data are true
Logical OR ( || ): As long as one of the two data is true, the result is true
Logical NOT ( ! ): Get the "opposite value" of a bool value
String operators:
There is only one concatenation operator ( + ): it means "concatenate two strings"
Distinguish the addition of arithmetic operators ( + ):
As long as one of the two data with the plus sign ( + ) is a string, it will be operated according to the "connection" meaning of the string. If the other is not a string, it will be automatically converted to a string before proceeding. Join operations.
Bitwise operators:
var v1 = 5; //This is decimal. The binary system is actually: 101. Inside the computer, it is actually similar to this: 00000101
var v2 = 6; //This is decimal. The binary system is actually: 110. Inside the computer, it is actually similar to this: 00000110
Bitwise AND:
symbol: &
Meaning: Perform an "AND operation" on the corresponding bits of two binary numbers, and the result is still the value represented by the binary number composed of the results of these bit operations.
Explanation: The rules for "AND operation" of binary numbers are:
1 & 1 è 1
1 & 0 è 0
0 & 1 è 0
0 & 0 è 0
Example:
var v1 = 5, v2 = 6, the operation diagram is:
v1=5
|
<🎜>0<🎜> | <🎜>0<🎜> | <🎜>0<🎜> | <🎜>0<🎜> | <🎜>0<🎜> | <🎜>1<🎜> | <🎜>0<🎜> | <🎜>1<🎜> | |||||||||||||||||||||||||||
<🎜>v2=6<🎜> | <🎜>0<🎜> | <🎜>0<🎜> | <🎜>0<🎜> | <🎜>0<🎜> | <🎜>0<🎜> | <🎜>1<🎜> | <🎜>1<🎜> | <🎜>0<🎜> | |||||||||||||||||||||||||||
<🎜>v1 & v2<🎜> | <🎜>0<🎜> | <🎜>0<🎜> | <🎜>0<🎜> | <🎜>0<🎜> | <🎜>0<🎜> | <🎜>1<🎜> | <🎜>0<🎜> | <🎜>0<🎜> |
var v3 = v1 & v2 = 00000100(2) = 4(10)
Bitwise OR:
symbol: |
Meaning: Perform an "OR operation" on the corresponding bits of two binary numbers, and the result is the value represented by the binary number composed of the results of these bit operations.
Explanation: The rule for performing "OR operation" on binary numbers is:
1 è 1
1 | 0 è 1
0 | 1 è 1
0 | 0 è 0
Bitwise NOT:
Symbol: ~ //This is a "unary operator"
Meaning: Perform "not operation" on the numbers in the corresponding bits of a binary number, and the result is the value represented by these binary numbers.
Explanation: The rules for "not operation" with binary numbers are:
Bitwise left shift operation:
Symbol: <<
Meaning: Move the digits on each bit of a binary number to the left by the specified number of digits, and ignore the "escaped" bits on the left (don't count), and fill in the empty bits on the right with "0" ”, the result obtained is the value represented by the binary number.
Example:
var v1 = 5; var v2 = 5 << 2; The operation diagram is:
v1=5 |
|
0
|
0
|
0
|
0
|
1
|
0
|
1
var v2 = 5 >> 2 = 00000001(2) = 1(10)
Other contents of operator:Assignment operator: In fact, it is a basic assignment operator: =, which means to put the data on the right side of the equal sign into the variable on the left side of the equal sign. Incorrect syntax: var v1 = 1, v2 = 2, v3 = 5, v4 = -2; v1 + v2 = v3 + v4; //Wrong, big mistake, big mistake Compound assignment operator: += -= *= /= %= The precedence of operators - too many, too complicated to remember, but please remember a few rules: 1. Be careful: operators have priority (sequence) issues. 2, Parentheses have the highest priority, and the equal sign (assignment) is the last. 3, Multiply and divide first, then add and subtract 4. If you don’t understand clearly or use brackets, use brackets 5, Parentheses are only parentheses, which can be nested layer by layer. Example: var year = 2013; // Determine leap year: If a year is divisible by 4 and not divisible by 100, it is a leap year, or if it is divisible by 400, it is also a leap year. if ( ( (year % 4 == 0) && (year % 100 != 0) ) || (year % 400 == 0) ) { document.write( year + "It's a leap year") ;} Data value transfer method:var v1 = 10; var v2 = v1; //Copy the middle value of v1 and put it into the variable v2 - this can also be said to be "pass the value of v1 to v2" var v1 = v1 + v1; //The value of v1 changes, but it does not affect v2 - because v2 itself is also an independent variable. document.write(" document.write(" The above "v2 = v1" is called "copy by value" - get the value of v1 and make a copy, and then assign it to v2 var v3 = {name:"小花", age:18, edu:"University"}; //This is a so-called "object" type - it includes 3 pieces of data. //Operation of the data is similar to this: document.write(" v3.age = 19; //The value of the age data of the v3 object is repeatedly assigned a new value, similar to the previous v1 = 20; var v4 = v3; //At this time, the "memory address" of the v3 variable is passed to the v4 variable, and the data of the v3 object itself is actually only obtained from this address. ——This is called pass-by-reference-by-value. At this time, the two variables actually have the same data content, similar to multiple names of a person: real name, stage name, screen name, nickname document.write(" v3.age = 20; // document.write(" Summary: In js, the basic data type uses "copy by value" - when the value is passed, a new data appears directly (of course it is also represented by another variable) Composite data types (arrays and objects) use "pass by reference" - when passing a value, you just make an address to point to, there is still only one copy of the data, and the two variables point to the same data.
if branch structure:Control structure: It is to use a certain syntax to control the flow of our program execution - it can also be called "process control structure" Branching is "multiple paths, take one". Simplest form : //If this condition is met (that is, the result of the conditional judgment is true), the statement block will be executed, otherwise nothing will be done. if( conditional judgment statement ) {//The statement block to be executed - the statement block is actually a general term for "n statements". }
Choose one branch form: The meaning is that there are two roads, one must be taken. if( conditional judgment statement ) {//Statement block 1 to be executed - executed when the previous condition is true } else { //Statement block 2 to be executed - executed when the previous condition is not true }
Multiple choice branch structure: Take one of the multiple paths depending on the conditions, but you may not take any of them: if(Conditional judgment 1) //If this condition is met, statement block 1 will be executed, and then the if will end{ //Statement block 1 } else if (Conditional judgment 2) //If condition 1 is not satisfied, then judge condition 2: If it is satisfied, execute statement block 2 and end if { //Statement block 2 } else if (Conditional judgment 3) //If condition 2 is not satisfied, then judge condition 3: If it is satisfied, execute statement block 3 and end if { //Statement block 3 } …………………… . . . . . If none of the conditions are met, nothing will be executed at the end and the if will end.
Comprehensive type: if(Conditional judgment 1) //If this condition is met, statement block 1 will be executed, and then the if will end{ //Statement block 1 } else if (Conditional judgment 2) //If condition 1 is not satisfied, then judge condition 2: If it is satisfied, execute statement block 2 and end if { //Statement block 2 } else if (Conditional judgment 3) //If condition 2 is not satisfied, then judge condition 3: If it is satisfied, execute statement block 3 and end if { //Statement block 3 } …………………… . . . . . else { //The last else statement block. //This statement block will be executed when none of the previous conditions are met. } Note: In the comprehensive form, there must be one branch (statement block) that will be executed.
switch branch structure: The overall meaning of the branch structure of switch is similar to that of if, which also means "take one of many paths". Its usage form:switch (a variable or expression) //Whether it is a variable or an expression, it ultimately represents a "value", we use v1 to speak { case Fixed value 1: //If v1 is equal to the value 1, statement block 1 will be executed // Statement block 1; break; //Exit this switch structure case Fixed value 2: If v1 is not equal to the previous value 1, continue to judge whether it is equal to the value 2 here. If it is equal, execute statement 2 // Statement block 2; break; //Exit this switch structure case ………………………………………… . . . . . . . . . . . . . . . . . default: // If the judgments in front are not established (that is, not equal), this sentence will be executed.
//Default statement block }
Special note: if statements can actually use very flexible conditional judgments, such as > >= < <= == != === , but there can only be one judgment for the case in switch :Equality judgment. while loop structure: Basic concepts of circulation:1, A loop is to execute certain programs (blocks of statements) repeatedly 2. There must be a way to stop the loop - the computer does not have its own judgment ability to decide "I'm tired, I won't do it anymore" Basic syntax form of while loop: while (conditional judgment) //If the conditional judgment is satisfied, the statement block will be executed, otherwise the while will end. { //The statement block to be executed } //If this statement block is executed, it will immediately return to the previous while position to continue judging.
The above forms are just the basic requirements of grammar, but in "practical" practice, you usually need to follow the following pattern: 【Loop variable initialization】 while ( [Loop variable as conditional judgment] ) { //The loop body is the statement block to be executed repeatedly 【Change of loop variable value】 } do while loop structure: The do while loop is a loop structure that is executed once first, and then determines whether to continue execution and loop based on judgment conditions.The practical form of do while is as follows: 【Loop variable initialization】 do { //The loop body is the statement block to be executed repeatedly 【Change of loop variable value】 } while ( [Loop variable as conditional judgment] ) ;
Description: First execute the statement block in brackets after do, and then perform the conditional judgment in the while statement. If the judgment is true (true), continue to return to the above do to execute the statement block in curly brackets, otherwise Finish.
for loop structure-recommended use: Basic form of for loop:for( [Initialization of loop variable A]; [Loop variable as conditional judgment B]; [Change of loop variable value C] ) { //Loop body D, which is the statement block to be executed repeatedly } Execution logic (sequence) of for loop: A è Bètrue è D è C è Bètrue è D è C è Bè ┗èfalseèfor loop ends ┗èfalseèfor loop ends Function Functions are not numbers.A function is just a grammatical construct. A function is a grammatical structure that "packages" several lines of code and uses them as a whole. A function can be said to be a syntax that needs to be defined and used - similar to a variable: it needs to be defined and used. A function is designed to accomplish a specific function and is usually a "function that needs to be executed repeatedly" - the purpose of a function is to execute the code blocks (statement blocks) wrapped in it A function is an independent execution space, that is, the code inside it can be considered "separated" from the code outside the function.
Definition form of function : function function name (formal parameter 1, formal parameter 2, ... ) { //The statement block to be executed - the function body } var s1 = parseInt(3.5); //3 var s2 = parseInt(3.8); //3 var s3 = parseInt(3.1); //3 var s4 = parseInt(3.0); //3 var s5 = parseInt(3); //3 ——parseInt is actually a function—a system internal function. This function has the "magical ability" to convert a number given to it into a corresponding integer, which is the largest integer not larger than the given number. Make a request: I want to find the hypotenuse corresponding to the two right-angled sides 3 and 4. I also want the hypotenuses corresponding to balls 5 and 6, as well as 7 and 8, and so on. . . . . . . .
What exactly is a function? Functions are just the grammatical embodiment of modular programming ideas - the so-called modular programming idea is to make various "repeated tasks" into independent "modules" and "call them directly" when used, thus saving the need for repeated tasks. The code to write.
Function calling form : Function name (actual parameter 1, actual parameter 2, ….. ); Function calling process: 1, First, the actual parameter values at the calling function are passed to the formal parameters at the function definition in one-to-one correspondence. a) The formal parameter must only be a "variable name", which represents a certain actual meaning - depending on the function function. b) The actual parameter must only be one "data" (may be direct data or variable data) c) Note: The variable name in the formal parameter cannot use var, nor can it be defined elsewhere. 2, Then the execution flow of the program enters the function, and "all statements" in the function are executed according to the appropriate flow 3. After the execution of the internal program of the function is completed, the execution flow of the program will return to the location of the function call. a) If the task performed by the function requires a return value, the return statement must be used to return the value. At this point the function naturally ends. b) If a function does not need to return a value, the return statement can also be used. At this time, it just indicates that the function will end here immediately (all statements may not be fully executed)
The return value of the function2 situations: There is a return value: it means that the function will obtain a data after a certain "calculation" (execution), and the data will be returned to the calling location (caller). At this time, you must use "return a certain value;" to achieve it. ——For example: if the boss takes a certain amount of money and tells an employee to buy a train ticket, the employee needs to return the purchased train ticket to the boss. Special attention: When there is a return value, the function call should be treated as a value. Whether a function returns a value is not determined by syntax, but by actual needs - the syntax only changes according to actual needs. No return value: Indicates that a function simply executes the code inside. It ends after execution and does not need to return a data to the "caller".
Parameters of function: There are no grammatical regulations at all. Instead, it is necessary to decide whether there are parameters or how many parameters there are according to the needs of the application - that is, to complete the function of the function, what should be provided Necessary data, this data is also reflected as the formal parameters of the function, each formal parameter represents a certain meaning (data).
Scope of variablesThe so-called scope refers to the "effective and available range" There are two types of scope: Global scope: A scope that is available (valid) in all program scopes. Local scope: A scope that can only be used (valid) within a specific scope (usually inside a function). Variables are divided into two types from the perspective of scope: Global variable: Refers to a variable that can be used in the global scope (valid). Variables defined outside a function are global variables. Global variables can be used both outside and inside functions. Local variable: Refers to a variable that can only be valid within a specific scope (usually a function) - usually inside the function in which the variable is defined System internal functionsparseInt(xxx): Convert parameter xxx into an integer - rounding operation. parseInt( 3 ); è 3 parseInt( 3.8 ); è 3 parseInt( 3.1 ); è 3 parseInt( “3” ); è 3 parseInt( “3.8” ); è 3 parseInt( “3.1” ); è 3 parseInt( “3abc” ); è 3 parseInt( “3.8abc” );è 3 parseInt( “3.1abc” );è 3 parseInt( “abc3” ); è NaN parseInt( “abc 3.8” );è NaN parseInt( “abc3.1” );è NaN parseInt( “abc” ); è NaN
NaN - It is a special number (type number), its meaning is: Not a Number - This situation usually means that a number is required but the data provided is not a number or cannot be When converted into a number. ——The number NaN is not equal to any number, including itself.
parseFloat(xxx): Convert parameter xxx to a decimal. parseFloat ( 3 ); è 3 parseFloat ( 3.8 ); è 3.8 parseFloat ( 3.1 ); è 3.1 parseFloat ( “3” ); è 3 parseFloat ( “3.8” ); è 3.8 parseFloat (“3.1”); parseFloat ( “3abc” ); è 3parseFloat ( “3.8abc” ); è 3.8 parseFloat ( “3.1abc” ); è 3.1 parseFloat ( “abc3” ); è NaN parseFloat ( “abc 3.8” ); è NaN parseFloat ( “abc3.1” ); è NaN parseFloat ( “abc” ); è NaN
Number(xxx): Convert parameter xxx into a "number" - pay attention to compare with parseFloat. Number ( 3 ); è 3 Number ( 3.8 ); è 3.8 Number ( 3.1 ); è 3.1 Number (“3”); è 3 Number ( “3.8” ); è 3.8 Number ( “3.1” ); è 3.1 Number (“3abc” ); è NaN Number ( “3.8abc” ); è NaN Number ( “3.1abc” ); è NaN Number ( “abc3” ); è NaN Number ( “abc 3.8” ); è NaN Number ( “abc3.1” ); è NaN Number ( “abc” ); è NaN isNaN(xxx): Determine whether the parameter xxx is a "not a number" - if it is a non-number, the result is true, otherwise if it is a number, the result is false isNaN (3); è false isNaN (3.8); è false isNaN ( 3.1 ); è false isNaN (“3”); è false isNaN (“3.8”); è false isNaN (“3.1”); false isNaN (“3abc” ); è è true isNaN (“3.8abc” ); è true isNaN (“3.1abc” ); è è true isNaN (“abc3”); è true isNaN (“abc 3.8”); è true isNaN (“abc3.1”); isNaN (“abc”); è true
|