First, data types
- Simple data types (Number, String, Boolean, Undefined, Null)
- Complex Data Types (Objects)
1. Variable declarations
- var a = b = c = 10;
That’s the same thing as var a = 10; b = 10; c = 10;B and C have no var declarations
- Var a = 10, b = 10, c = 10;
- Var a, b, c = 10
2. Simple data types
2.1 Number
- Octal: 0 before digit hexadecimal: 0x before digit
- NaN: non-numeric isNaN() : verifies whether it is non-numeric, returning false if it is numeric and true otherwise
2.2 String
- String escape character \
- Gets the string length: string.length
- String concatenation: + string + any type = string \
- String numbers are compared from the first digit: ‘121’<’25’, so the numeric type \ is converted first
- The string itself is immutable. Changing the value of the string creates a new memory space to store the new value. The original string is not changed
2.3 undefined and null
- undefined
- If a variable has no value assigned, it is undefined
- Undefined + ‘I’m a string’ = undefined ‘I’m a string’
- Undefined + number = NaN
- Null null values
- Null + ‘aaa’ = null I am string ‘
- Null + number = number
3. Obtain the data type typeof variables
Typeof variable
4. Data type conversion
4.1 Converting to a string
The conversion mode of plus splicing is implicit conversion
4.2 Converting to Alphanumeric
- ParseInt returns an integer, removing the units after the number
- ParseFloat is a floating point number, and it gets rid of the units after the number
- Number (variable)
- Use the arithmetic operation – * /
4.3 Convert to Boolean
Boolean(variable) : represents null, negative values are converted to false, others to true
5. Data type memory allocation
5.1 Memory can be viewed as divided into two parts: stack and heap
- Simple data types are stored on a stack, holding the value itself (the value type)
- Complex data types first store the address in the stack, which refers to the data in the heap (reference type).
5.2 Parameter Transfer for Simple or Complex Types
- Simple type parameter passing: When you pass a value variable as an argument to a function parameter, you are actually making a stack copy of the value of the variable to the parameter, so changes to the parameter inside the function do not affect the external variable
- Complex type parameter passing: essentially copies the stack address of the variable to the parameter. The parameter arguments hold the same heap address, so they operate on the same object
Second, operators
- Arithmetic operator
- Increment ++, decrement — operators
- Comparison operator
- Logical operator
- The assignment operator
1. Increment ++, decrement — operator
- Increment in front, decrement ++a
Add first, then return the value
- Increment after, decrementing a++
The expression returns the original value, and the variable appends itself
2. Compare operators
- == requires only equality, and converts the data type by default
- === requires that the values and data types on both sides be exactly the same
3. Logical operators
Logic interrupt (short circuit operation)
- Logic and expressions && expressions If the previous expression is true, the result is the value of the second expression; If the previous one is false, it is the first value
- Logic or expression | | expression
If the previous expression is true, the result is the value of the first expression; If the former is false, it is the second value
4. Operator priority
- parentheses
- The unary operator ++ –!
- The arithmetic operators are preceded by * / % and followed by + –
- Comparison operator
- First > >= < < <=
- After the = = = = =! =! = =
- Logical operators && before | |
- The assignment operator
3. Process control
1. Sequential structure
- if… else if… else
- switch
If there is no break in the case statement in the switch loop, the next case statement continues until a break occurs
2. Loop structure
2.1 Double for loop
- Print ⭐ case using string appending
- Print inverted triangle cases
- The multiplication table
2.2 the while loop
- Type I love you end, otherwise keep asking
2.3 Do while loop
-
Execute the statement in do first, then judge
-
The do while loop is executed at least once
The simple ATM case
- The while loop
2. do… The while loop
2.4 the continue
- Exit the loop and execute the next loop
2.5 break Exits the loop
Four, arrays,
1. Array length, traversing the array
The length of an array is the number of elements in the array
Iterate over groups using the for loop
2. Add an element to the array
Filter array: Filter elements from the old array and append them to the new array
- Using a variable with an initial value of 0, increments the variable by 1 each time an element is appended to a new array
- NewArray. The length is 0
3. Bubble sort
Two for loops are used, with the outer for loop representing the number of trips and the inner loop representing the number of comparisons per trip
Five, the function
1. Declaration of functions
-
The function declaration
Function name (){function body... }Copy the code
-
Functional expression
Var var name () = function(){function body... }Copy the code
2. Parameters of a function
Function arguments and arguments
- A parameter can be thought of as an undeclared variable. The argument passed to the parameter is used as an assignment to the parameter. The default value is undefined
- Parameter number mismatch:
- Same number: returns the correct result
- More arguments than parameters: the number of parameters to be taken (i.e., the extra parameters are ignored
- Fewer arguments than parameters: parameters that do not receive real parameters default to undefined. The result is: real parameter passed +undefined
3. Return value of the function: return
- Equivalent to assignment: the result after function name () = return
- Return interrupts the function, and the following code does not execute
- Return returns only one value. If multiple values follow a return, only the last value is returned
- If there are multiple values to return, return[x1,x2,x3]
- If there is no return, undefined is returned
4.arguments
- Used when you do not know the number of arguments
- Arguments is a built-in object for the current function, which stores all arguments passed
- Arguments presentation form is a pseudo-array
- Has the length property
- Store data by index
- It’s not really an array, it doesn’t have push,pop, etc
- Take the maximum of any number
5. The scope
Variables inside a function are local variables or global variables if they are not declared by var
6. Preliminary parsing
-
The js engine runs JS in two steps: pre-parses the code to perform pre-parses: the JS engine pushes all the var and function functions in the JS to the front of the current scope
-
Preparsing is divided into variable preparsing (variable promotion) and function preparsing (function promotion)
- Variable preresolution: promotes all variable declarations to the front of the current scope, without promoting assignment operations
- Function pre-resolution: promotes all function declarations to the front of the current scope, without calling the function
Six, objects,
1. User-defined objects
1.1 Call object properties and methods
- Invoke properties of the object
- Object name. Attribute name
- Object name [‘ Property name ‘]
- Method that calls an object: object name. The method name ()
1.2 Creating Objects
-
Create objects using object literals
-
Create an Object with new Object
-
Constructors create objects (instantiation of objects) : can be batch generated
- This can’t throw
- As soon as new Hero() calls the function, an object is created
1.3 the for… in… Traverse object
k–key
2. Built-in objects
2.1 Document Reviewdeveloper.mozilla.org/zh-CN/
2.2 Math Random number
2.3 Date Date object
What day is it today
2.3.2 timestamp
Countdown case
2.4 Array built-in objects
2.4.1 Adding and deleting array elements
Filter array elements
2.4.2 Array sort sort
2.4.3 Array Indexes
-
IndexOf () returns -1 if the first indexOf a given element does not exist
-
LastIndexOf () finds that the lastIndexOf a given element does not exist and returns -1! [image – 20210527164310449. The PNG] (p1-juejin.byteimg.com/tos-cn-i-k3…
1. Array deduplication
2. Search for the position and number of occurrences of a character in the string