JavvScript(High-level programming language)

The three components of JS are 1 ECMAScript — JavaScript syntax 2 DOM — page document object model 3 BOM — browser object model

Computer languages Machine languages (binary languages that can be interpreted and executed directly by computation), assembly languages, and high-level programming languages

The style of the js

1. Inline JS

<input type="button" value =Onclick = "alert (' hello, world ')" />Copy the code

2. Embedded JS

<script>alert(' 'hello,world): </script>
Copy the code

3. Reference the JS file externally

<script src="my.js"></script>
Copy the code

Single-line comments: command+/ or add // multi-line comments: / end with /

Alert (MSG) The browser displays an alert box console.log(MSG) The browser console prints an output message and writes a code to check whether the code executes prompt(info) The browser displays an input box for the user to enter

variable

Variable: a container for storing data. Data can be obtained by the name of the variable, and even the var age of the data can be modified. Age Specifies the variable name assigned by the programmer age=10; // Assign this variable to 10 output console. The log (age); Var age = 10; Initialization of a variable

The data type

Number: integer and decimal Boolean: true, false, equivalent to 1 and 0 String: It is a string, for example, Undefined: var a; Var a= Null; var a= Null; The variable a is null and octal is preceded by 0, e.g. 012 is 10. The hexadecimal prefix is 0x, the value is 0-9, a-f. 0x=11 Infinity -Infinity NaN not a number isNaN (x) x is a number, return false; X is not a number, return true.

identifier

An identifier is the name identifier of a variable, function, attribute, or function argument: the first character must be a letter, underscore (_), or dollar sign ($); Use strict “typeof type indicates the typeof data, such as string

STR nested

String quote nesting (must be quoted) js can use single quotes to nest double quotes or (outside double inside single, outside single inside double) string escape character \n newline character, n stands for newline; \ slash \; ‘single quote’; “Double quotation marks”; \t Indent TAB; Var STR =’names’; alert(str.length); Console. log(’12’+12)//1212; console.log(’12’+12)//1212; Console. log(12+12)//24 var age=18 console.log(‘pink teacher ‘+age+’ age ‘) Typeof Var num=10; console.log(typeof num); //number instanceof check what type of object is the reference value or object true, check the original value false

Data type conversion

ToString () var num=1; Console. log(num toString()) String() Cast var num=1; Console. log(String(num)) var num=1; Console. log(num+” string “) 2. Convert parseInt(string); Change string to integer value parseInt(’78’) parseFloat(string); Cast parseFloat(‘78.21′) Number(); Number(’12’) js implicitly convert (’12’ -0) parseInt and parseFloat to uppercase, lowercase, 3. Boolean() means that null and negative values are converted to true.

Operators Operators

Increment and decrement operators: (++) or (–) written in front of the variable before the first add 1 or subtract 1, after the return value after the increment, first return the original value, then add the comparison operator < > sign; > >; >= The greater than or equal to sign; <= less than or equal to sign; == judge equal sign (will transform); ! = inequality; = = = are congruent; Logical operators && “logical and” and; | | the logical “or” the or; ! 1. Logic and: syntax: expression 1 && expression 2; If the value of the first expression is true, expression 2 is returned and if the value of the first expression is false, expression 1, 2 is returned. Logic or: grammar: expression 1 | | expression 2; If the result of expression 1 is true, return expression 1; if the result of expression 2 is false, return expression 2 3 Logical non: False if the result of expression 1 is true if the result of expression 2 is false

Process control

Three types of structure: sequential structure, branch structure, circular structure order: unary operation logic is not high priority logic and higher or higher priority logic

1. Branch flow controls the idea of if statement execution; If the condition in if is true, execute the statement inside the braces. If the condition in if is false, do not execute the statement inside the braces. Execute the code following the if statement. } else if(conditional expression 2) {// statement 2; } else {3} / / statements only alternative 1, such as 1 don’t meet and then down, see 2 can’t satisfy, don’t satisfy the else statement

2. Ternary expressions: Expressions? Expression 1: Expression 2 returns the value of expression 1 if the expression is true, and expression 2 if the expression is false.

3. Branch flow control switch statement switch(switch, switch) switch(expression){expression matches value1, value2; Case Value1: Statement 1 is executed. Break; Case value2: execute statement 2; Break; Default: Execute the last statement. } case meaning of small examples or options; If the case in the switch does not break, it does not exit and continues to execute the next case

cycle

Loop: Regular operation, loop can repeatedly execute certain statements

For loop: the statement that is executed repeatedly is the body of the loop and can continue execution depending on the termination condition. The two of them make up a loop. For (initialize a variable; Conditional expression; Var declares a common variable, usually used as a counter, 2. The condition expression is used to determine whether the loop continues each time, and is the termination condition 3. Action expressions are the code executed at the end of each loop and are often used in our counter variables such as: for(var I = 1; I < = 100; I++){console.log(‘ how are you? ‘); } output 100 times how are you? For loop output if displayed on one line: append string var STR = “; for(var i = 1 ; i <=5; I++){STR = STR +” want to loop text “; (Then the output is on one line)}

Double loop for (outer initializer variable; Outer conditional expression; Outer operation expression) {loop body; For (inner initialization variable; Conditional expression of inner layer; {// Execute statement (loop body)}}

While (conditional expression){// loop body; } there is also an operation expression: i++

Do {// loop body; i++}while(i <= 100)

Continue: Exits the current loop and continues with the next loop break: exits the entire loop and the subsequent loop is not executed

Sources breakpoint debugging, find the file to debug, and then find the line of code to debug

Array (Array)

An array is a collection of data, each of which is called an element. An array can hold elements of any type. Arrays are an elegant way to store a set of data under the name of a single variable. Var num =new Array(); var num =new Array(); // Create an empty array

Create an array using an array literal

Array name =[];var=[' first array element ',2.'pink'.true]; Index number:0.1.2.3  // index 0 is the firstIndex of an array: array name [index number [; index number from0The start of theCopy the code

Iterate through an array: Accesses the elements of an array from beginning to end once

var arr = ['red'.'green'.'blue'];for(var i=0; i <3; i++){console.log(arr[i]); }Copy the code

Array length Arr.length Specifies the array name. Length dynamically detects the array to output multiple variables, separated by commas such as console.log(Sun,average)

Add elements in array 1. Add var arr=[1.2.3.4.5] by modifying length. Arr [3]= ‘pink’; arr[3]= ‘pink’; arr[3]= ‘pink’; You cannot assign to the name of the array directly, otherwise the previous data will be overwritten

Var arr[5,1,2,4,3] for(var I = 0; i <= arr.length ; i++){ for(var j = 0 ; j < arr.lenth-i-1; j++){ if(arr[j] > arr[j+1]){var temp = arr[j] ; arr[j] = arr[j+1] ; arr[j+1]=temp } }}

function

A function is a block of code that encapsulates a call that can be executed repeatedly. (function name () {// function body}) Inside the parentheses of a declared function are parameters, inside the parentheses of a function call are arguments. Parameters are parameters that take arguments (passed arguments). Return Return value: return returns the value to the function caller

Preliminary analysis

The array object

Add and remove array elements

1. Push () adds one or more array elements to the end of our array

var arr = [1.2.3];
arr,push(4.5.6.'red')
console.log(arr);
Copy the code

2. Unshift () At the beginning of our array, add one or more arrays (unshift is similar to push)

3. Pop () removes the last element of the array. There are no arguments in pop, and only one element can be removed at a time

4. Shift () deletes the first element of an array

The sorting

Array index return

Returns characters based on position