I. Introduction to JavaScript
JavaScript is a literal-translation scripting language, which is a dynamic type, a weak type, a prototype-based language, and a built-in support type. Its interpreter, known as the JavaScript engine, is widely used as a scripting language for the client, and is used in HTML (an application under the standard Universal Markup Language) to add dynamic functionality to an HTML Web page.
Two. Introduction of JavaScript base 2.1JS
<! DOCTYPE html>
<! DOCTYPE html>
2.2 JS variable, solid and identifier 2.2.1.JS variablesX=5y=6z=x+y
In algebra, we use letters (such as x) to hold values (such as 5). By the above expression Z=x+y, we can calculate the value of Z as 11. In JavaScript, these letters are called variables.
So how to define the use of variables in JS?
1. Declare a variable without declaring the variable type. All use the var keyword;
var a;<br>a=3;
2. a row can declare multiple variables. And can be of different types
var name= "Yuan", age=20, job= "lecturer";
3. you can declare variables without var. if you don't use Var then it's a global variable
2.2.2 Constants and identifiersConstants : Data values that appear directly in the program
Identifier:
- Consists of letters, numbers, underscores (_), Dollar signs ($) that do not begin with a number
- A name commonly used to denote functions, variables, etc.
- For example: _ABC, $ABC, abc,abc123 is an identifier, and 1ABC is not
- Words that represent a particular meaning in the JavaScript language are called reserved words and do not allow programs to be redefined as identifiers
Data type of 2.3JS/ * Number-----The value Boolean ----- boolean string ----- string undefined ----- undefined null ----- NULL *
2.4 operatorArithmetic operators: + - * / % + +- comparison operators: > >= < <= ! = = = = = = = = !== logical operator: && | | ! Assignment operator: = = = = *= /= string operator: + join, either side operand has one or two is a string do join operation
2.4.1 Arithmetic operatorsNote 1: Self-added self-reduction
If x=2, then the value of the X + + expression after the execution of the 3,x--expression is 1;i++ equivalent to i=i+1,i--equivalent to i=i-1;
Increment and decrement operators can be placed before variables or after variables:----
var i=10;console.log (i++); Console.log (i); Console.log (++i); Console.log (i); Console.log (i--); Console.log (-I.);
NOTE 2: Cell operators
-In addition to being able to represent a minus sign, you can also indicate a minus sign . For example: x=-y+ addition can be used to indicate a connection to a string for example: "abc" + "def" = "abcdef"
Note 3:
var d= "Yuan"; D=+d; alert (d);//nan: A special value of type number, which gets a NaN data alert (typeof (D)) when it encounters an invalid to convert a string to a digit ;//number //nan features: var n =nan;
alert (n>3); alert (n<3); alert (n==3); alert (N==nan); alert (N!=nan); All operations that//nan participate in are false, except! =
2.4.2 comparison Operators> >= < <= = = = = = = = !==
When using a control statement:
if (2>1) { Console.log ("condition is set!") }
equals and non-equal operators are all equal and non-full equals. The two operators do the same as equals and non-equals, except that they do not perform type conversions until they are checked for equality.
Console.log (2==2), Console.log (2== "2"), Console.log (2=== "2"), Console.log (2!== "2");
Note 1:
varBresult = "Blue" < "Alpha"; alert (bresult);//Output TrueIn the example above, the string "Blue" is less than "alpha" because the character code of the letter B is 66, and the character code for the letter A is 97.. Comparing numbers and strings another tricky situation occurs when you compare numbers in the form of two strings, such as:varBresult = "3" < "; alert (bresult);//output "true"The above code compares the strings "25" and "3". Two operands are strings, so the comparison is their character code ("2" character code is 50, "3" character code is 51). However, if one of the operands is a number, the result is interesting:varBresult = "3" <; alert (bresult);//output "false"here, the string"25" will be converted to the number 25, then with the number 3compared, the results were expected. Summary: Comparison operators on both sides if one is a numeric type and one is a different type, its type is converted to a numeric type. If both sides of the comparison operator are of type string, the ASC code of the highest bit is compared, and if the highest bit is equal, continue to take the second bit comparison.
View CodeNOTE 2:
equality operators: The rules for performing type conversions are as follows: If an operand is a Boolean value, convert it to a numeric value before checking for equality. false to 0,true to 1nullnullfalse Truetrue, otherwise two operands are not equal.
View Code2.4.3 logical operatorsif (2>1 && []) { Console.log ("Conditions and")}console.log (1 && 3);//3console.log (0 && 3);// 0console.log (0 | | 3);//3console.log (2 | | 3);//2
2.5 Process Control
- Sequential structure (executed from top to bottom)
- Branching structure
- Loop structure
2.5.1 Sequential structure
<script> console.log ("Monday"); Console.log ("Tuesday"); Console.log ("Wednesday"); </script>
2.5.2 Branching structure
If-else structure:
if (expression) { statement 1; ...... } else{ statement 2; ..... }
Function Description: Execute statement 1 if the value of the expression is true, otherwise execute statement 2
Example:
var x= (new Date (). GetDay ()); // get today's week value, 0 for Sunday var y; if ((x==6) | | (x==0)) { y= "Weekend";} Else { y= "weekday"}console.log (y); // equivalent to y= "weekday"if (x==6) | | (y==0)) { y= "Weekend";} Console.log (y);
View Code
if-elif-else Structure:
if (expression 1) { statement 1;} else if (expression 2) { statement 2;} else if (expression 3) { statement 3;} else{ statement 4;}
Example:
var score=window.prompt ("Your score:")if (score>90) { ret= "excellent"} Else if (score>80) { ret= "good"}Elseif (score>60) { ret = "Pass"}else { ret= "Failed"}alert (ret)
View Code
switch-case Structure :
Switch basic format switch (expression) {case value 1: statement 1;break; Case value 2: statement 2;break; Case Value 3: statement 3;break; Default: statement 4;}
Example:
<! DOCTYPE html>
Switch is more concise and clear than the else if structure, making the program more readable and more efficient.
2.5.2 Cycle StructureFor loop:
Syntax rules: for (initial expression; conditional expression; self-increment or decrement) { Execute statement ... }
Function Description: Implement the condition loop, when the condition is established, EXECUTE statement 1, otherwise jump out of the loop body.
Another form of A For loop:
For (variable in array or object) { Execute statement ... }
While loop:
Syntax rules: while (condition) { statement 1; ... }
Function Description: Run function and for similar, when the condition is formed loop execution statement curly braces {} Inside the statement, otherwise jump out of the loop; also support continue and break statements.
Example:
var i=1;while (i<=7) { document.write ("
2.5.3 Exception Handling
try { //This piece of code runs from top to bottom, where any one of the statements throws an exception the code block ends running}catch (e) { ///If an exception is thrown in a try code block, the code in the catch code block is executed. //e is a local variable that is used to point to an Error object or other thrown object}finally {//Whether or not the code in the try is thrown (even if there is a return statement in the try code block), the finally code block is always executed. }
Note: Throw exception throw error (' xxxx ')
JavaScript for the front-end base