JavaScript
1. Characteristics of JavaScript
- Interpretive language
- Syntax structure similar to C and Java
- Dynamic language
- Prototype based object oriented
2. Variables
2.1. What are variables
Variables are "containers" for storing information.
2.2. Variable name specification
- Variable must start with a letter
- Variables can also be expressed as $and_ Start with symbol (not recommended)
- Variable names are case sensitive (Y and y are different variables)
2.3. Declaration of variables and assignment
1. Declare variables with var keyword
var a;
2. Assignment
var a; a ="tel";
var a ="tel";
3. Multiple variables in one statement
var name="Do", age=27, job="doctor";
3. Data type
3.1. Value type (basic type)
String (string)
var carname="Good LUCKY60"; var carname='Good LUCKY60';
Escape character
- /b back one space
- /f page change
- /n line feed
- /r return
- /t tabulation
- /'single quote
- /Double quotes
- //Backslash
Number
var x1=34.00; //Write with a decimal point var x2=34; //Write without decimal point var y=123e5; // 12300000 var z=123e-5; // 0.00123
Extremely large or small numbers can be written by scientific (exponential) counting
Boolean
var x=true; var y=false;
Boolean (logic) can only have two values: true or false
Null and Undefined
person=null;
Undefined this value indicates that the variable does not contain a value.
You can empty a variable by setting its value to null.
Symbol
Note: Symbol is a new raw data type introduced by ES6, which represents a unique value
3.2. Reference data type
1. Object
Objects are separated by curly braces. Inside the braces, the attributes of the object are defined in the form of name and value pairs (Name: value), and the attributes are separated by commas
var person={firstname:"Mary", lastname:"Lin", id:1234};
There are two ways to get:
name=person.lastname; name=person["lastname"];
2. Array
Two declaration assignment methods:
var arr=new Array(); arr[0]="one"; arr[1]="two"; arr[2]="there";
var arr=new Array("one","two","there");
3. Function
3.3. Declaring variable types
var name=new String; var n=new Number; var y=new Boolean; var arr=new Array; var person=new Object;
4. Notes
Single line note://
Multiline comment: start with / * and end with * /
(Note: HTML comment symbols start with < -- and end with -- >)
5. Operator
5.1. Operator priority
Priority from high to low
- () highest priority
- Unary operator + + –!
- Arithmetic operator first * /% then +-
- Relational operator > > = <<=
- Equality operator = =! = = ======
- Logical operator & & before||
- Assignment Operators
5.2. Arithmetic operators
+ - * / %
5.3. Unary operator
Pre + +: add 1 before operation
Post + +: calculate first and then add 1
Pre --: subtract 1 before operation
Post --: operation first, then minus 1
5.4. Logical operator (Boolean operator)
&& //When the logic and two operands are true at the same time, the result is true, otherwise both sides and one side are false || //If one of the logical or two operands is true, the result is true; otherwise, it is false ! //Logical negation
5.5. Relational operator (comparison operator)
< > >= <= == != === !==
==Difference from = = =:
==Only values are compared. If the values are equal, the result is true
===The equality result is true only when the type and value of the value are equal at the same time
5.6. Assignment operator
= += -= *= /= %=
6. Expressions and statements
6.1. Expression
An expression can produce a value, which may be an operation or function call, or a literal. An expression can be placed wherever a value is required.
6.2. Statement
Statement tells the browser what to do
A program consists of many statements. Generally, semicolons are used to separate JavaScript statements
6.3 statement identifier
JavaScript statements usually start with a statement identifier and execute the statement.
Statement identifiers are reserved keywords and cannot be used as variable names.
break //Used to jump out of a loop (jump out of the entire loop). catch //Statement block, which executes the catch statement block when there is an error in the execution of the try statement block. continue //Skip an iteration in the loop (jump out of the current loop layer). do ... while //Execute a statement block and continue to execute the statement block when the conditional statement is true. for //When the conditional statement is true, the code block can be executed a specified number of times. for ... in //Used to traverse the properties of an array or object (loop through the properties of an array or object). function //Define a function if ... else //Used to perform different actions based on different conditions. return //Exit function switch // Used to perform different actions based on different conditions. throw //Throw (build) error. try //Realize error handling and use it with catch. var //Declare a variable. while //When the conditional statement is true, the statement block is executed.