>>>js Basic Knowledge
First, JS role
1, form verification, reduce the pressure on the service side;
2. Add a page animation effect
3. Dynamically change the content of the page
※4, Ajax network requests
II. Basic Structure
<script type= "Text/javascript" >
Alert ("hahaha");
</script>
Three, three ways to use JS
1, directly in the HTML tag, using the event properties, invoke the JS code:
<button onclick= "alert (' Dot me! ‘);" > Point me! </button>
2. In any position on the page, use the script tag to insert the JS code.
<script type= "Text/javascript" >
Alert ("hahaha");
</script>
3, the introduction of external JS file:
<script src= "Js/01.js" type= "Text/javascript" ></script>
[ precautions ]
The ①JS code can be used anywhere on the page, but the placement is different, which will affect the order of JS execution.
② introduction of External JS in the script tag, no longer contain any JS code.
Iv. variables in JS
1. Declaration of variables
var num = 1; variables declared with Var, which are local variables, are only active at the current
num = "hahaha"; variables declared without VAR, default to global variables, available in the entire JS file
var a=1,b,c=2; Declare multiple statements using one line of code. where B is undefined
[Considerations for variable declarations in JS]
The keyword that declares a variable in ①js has only one Var, the type of the variable, depending on the value assigned;
If the declaration is an assignment, then the undefined type.
The same variable in ②js, can be modified in multiple assignments, the data type;
var num1=1;
num = "string";
the ③ variable can be declared with Var, or it can be directly assigned;
[difference] Use VAR to declare the scope to be a local variable.
④ in JS, a variable can be used multiple times VAR declaration, the subsequent declaration is equivalent to direct assignment, without any effect;
⑤JS variables are case-sensitive, uppercase and lowercase are not a variable;
2, JS data type:
Undefined: Variables declared with VAR, but not assigned
Null: Represents an empty reference
Boolean: TRUE or False
Number: Numeric type, including integral and floating-point types
String: Strings
Object: Objects
3. Common numerical functions
① IsNaN: Used to detect is a variable, is not a non-numeric (not a number);
isNaN when detected, the number function is called first, attempting to convert the variable to a numeric type, or Nan if the final result can be converted to a numeric value.
② number function: Used to convert various data types to numeric types
>>>undefined: Cannot convert, return nan;
>>>null: converted to 0;
>>>boolean:true converted to 1,false to 0;
>>> string:
If the string is a pure numeric string, it can be converted, "123"-->123
If the string contains non-numeric characters, it cannot be converted, "123a"-->nan
If it is an empty string, it is converted to 0, "-->0" "-->0
③ parseint (): Converting a string to a numeric type
>>> if it is an empty string, cannot go, ""-->nan
>>> if it is a pure numeric type string, it can be converted, and the decimal point is left out, not reserved, "123"-->123 "123.9"-->123
>>> If the string contains non-numeric characters, the integer preceding the non-numeric character is converted, "123a"-->123 "A123"-->nan
④ parsefloat (): The conversion mechanism is the same as in Java.
The difference is that when converting a numeric string, the decimal point can be preserved if the string is a decimal, "123.5"-->123.5 "123"-->123
⑤ typeof (): detects the data type of a variable.
String->string numeric->number True/false->boolean
->undefined object/null->object function not defined->function
Five, JS commonly used in the input and output statements
1.alert (): pop-up window output
2,prompt (): pop-up window input
Accept two-part parameter: ① input prompt content; ② The default text of the input box. (both parts can be omitted)
The input content is a string by default.
3,document.write("
Print on the browser screen.
4,console.log("hahaha");
Browser console printing.
The operators in the six, JS
1, Division sign: whether the symbol on both sides is an integer or a decimal, except after the end will be in accordance with the actual results to retain decimals ;
22/10-2.2
2, = = =: The data, type and value must be the same on both sides of the equals sign. Returns false directly if the type is different
= =: Only judge the data on both sides, the value is equal, do not care whether the two sides of the equation is the same data type
! =: Unequal! = =: not congruent
3, &, | Bitwise operations can only be performed, if the two sides are not numeric types, will be converted to numeric types and then operations;
&&, | | Perform logical operations
4. The level of operation of the operators at all levels:
>>>js branching and looping
First, if judgment
1, JS in the True and false judgment:
①boolean type: True for True, false for false;
② value type: 0 is false, not 0 is true;
③ String Type: "" is false, non-empty string is true;
④null/undefined/nan: All is false;
⑤object: All is true.
2. If judgment:
Second, the circulation
1. Switch
Various data types can be placed in the () of the switch structure:
When compared, the "= = =" is used to determine that the data type is exactly equal
JS The difference between switch and switch in Java:
Java in the switch can not judge the interval, and JS in the switch to determine the interval ↓↓↓
2, Do-while
do{
}while (FALSE);
3. For loop
for (Var i=0;i<100;i++) {
}
4, Example: Enter a number, determine whether it is a positive integer, if it is not a positive integer, prompt input error, please re-enter, if it is a positive integer, invert the output of this number.
JavaScript basic knowledge must know!!!