Operator:

  • Pre – and post-increment operators:

Num ++ returns the original value and then increments by one

  • Comparison operator

18 == '18' (true)     18 === '18' (false)

  • Logical operator

    Logic and && logic or | | logic not!

_ Short-circuit operation: _

If the value of the first expression is false, return 1. Example: console.log(123 && 456); // 456 console.log(0 && 456); // 0 console.log(0 &&1 + 2 && 456 * 56789); / / 0 2. Logic or syntax: expression 1 | | expression if the value is true, the first expression expression returns 1 if the first value of the expression is false, it returns expression in 2 cases: the console. The log (123 | | 456); //123 console.log(0 || 456); // 456 console.log(123 || 456 || 789); / / 789Copy the code

Branching structure

1. If statement:

If (conditional expression) {execute statement} if (conditional expression) {execute statement 1} else {execute statement 2} if (conditional expression 1) {statement 1} else if (conditional expression 2) {statement 2} else if (conditional expression 3) { Statement 3} else {statement 4}Copy the code

2. Ternary operators

Conditional expression? Var num = 10; var result = num > 5 ? 'Yes' :' no ';Copy the code

3. The switch statement

Switch (expression) {case value1: execute statement 1; break; Case value2: execute statement 2; break; . Default: Execute the last statement. } // The expression must be identical to valueCopy the code

cycle

1. The for loop

for ( var i = 1; i <= 100; I++){console.log(' how are you '); }Copy the code

2. The while loop

van num = 1; While (num <= 100) {console.log(' how are you '); num ++; }Copy the code

3. The do while loop

var i = 1; Do {console.log(' how are you '); i++; } while (i <= 100)Copy the code

An array of

1. Create an array

Var arr = new Array(); Var arr = []; Var arr = [1, 2, 3, 4];Copy the code

2. Array length

arr.length()
Copy the code

3. Add array elements

Var arr = ['red', 'green', 'blue']; arr.length = 5; console.log(arr); // ['red', 'green', 'blue', undefined, undefined] 2. Var arr = ['red', 'green', 'blue']; arr[3] = 'pink'; arr[4] = 'hotpink'; console.log(arr); // ['red', 'green', 'blue', 'pink', 'hotpink']Copy the code

function

1. Declare functions & call functions

Function Function name (parameter 1, parameter 2...) Function sayHi() {console.log('hi~~'); } // call the function sayHi(); -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- / / statement function 2 - function expression var variable name = function () {}; Var fun = function(){console.log(' I am a function expression '); } // Call function 2 fun();Copy the code

2. Function parameters

function getSum(num1,num2) { console.log(num1 + num2); } // if the number of arguments is more than the number of parameters get(1,2,3); // If the number of arguments is less than the number of parameters, undefined get(1) is taken; //num1 = 1 num2 = undefined The output is NaNCopy the code

3. Return value of return

Function fn(num1, num2) {return num1, num2; } fn (1, 2); // 2 // return To return multiple values, Function getResult(num1,num2) {return [num1 + num2, num1-num2, num1 * num2, num1 / num2]; } // if there is no return, undefined is returnedCopy the code

4. Arguments

You can use arguments to retrieve arguments when you are not sure how many arguments to pass into a function.

function fn() { console.log(arguments); console.log(arguments.length); console.log(arguments[2]); Arguments for (var I = 0; i < arguments.length; i++) { console.log(arguments[i]); }}Copy the code

Global variables Local variables

Just remember: variables that are not directly assigned are global variables if they are inside a function

Preliminary analysis

The JS engine runs JS in two steps: pre-parsed code execution

1. Pre-parsing: The JS engine will push all var and function functions in JS to the front of the current scope

Code execution: Executes from top to bottom in the order code is written

2. Pre-parsing is divided into variable pre-parsing (variable promotion) and function pre-parsing (function promotion)

(1) Variable promotion is to promote all variable declarations to the front of the current scope without promoting assignment operations

console.log(num); Var num = 10; fun(); // An error will be reported saying the function is undefined, which is equivalent to executing the code: var fun; var fun = function(){ fun(); console.log(22); fun = function() { } console.log(22); } // So the function expression call must unload below the function expressionCopy the code

(2) function promotion promotes all function declarations and definitions to the top of the current scope

fn(); Function fn() {console.log(1); }Copy the code

object

1. Create an object

Var obj = {uname: 'zonefeng ', age:18, sex: SayHi: function() {console.log('Hi~'); }} // Call the property of the object: the object name. Property name or object name [' Property name '] console.log(obj.uname); console.log(obj['uname']); // Call the method sayHi object name. Method name () obj.sayhi (); -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- / / 2. Var obj = new Object(); // Use assignment to add object and method obj.uname = 'zhang SAN '; obj.age = 18; Obj. sex = 'male '; obj.sayHi = function() { console.log('hi~'); } -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- / / 3. Function Constructor name () {// this. Attribute = value; Function () {//} function Star(uname, age, sex) {// This. Name = uname; this.age = age; this.sex = sex; this.sing = function(sang) { console.log(sang); }} var LDH = new Star(' dev ', 18, 'dev '); // Call constructor must use new ldh.sing(' freezing rain ');Copy the code

2. Traverse the object

//for in loop for (var k in obj) {console.log(k); Console. log(obj[k]); // obj[k]Copy the code

3. Built-in objects

Math

Date

Array

Var arr = []; console.log(arr instanceof Array); console.log(Array.isArray(arr)); //2. Reverse array arr.reverse(); //3. Array sort arr.sort(); Arr.sort (function(a,b){return a - b; // return b - a; / / descending});Copy the code

String

Replace var = 'andyandy' str.replace('a','b'); Var str2 = 'red, pink, blue'; str2.split(','); / / [' red ', 'pink', 'blue'] / / conversion case STR. ToUpperCase (); str.toLowerCase();Copy the code