Parsing process and variable promotion

Parsing: The code is parsed before execution

consloe.log(web);
var web = 'pikasu';
The output is: UNDpay
Copy the code

Because the analytic variable is promoted after

varThe web;// The variable is promoted
consloe.log(web);
 web = 'pikasu';
'So the output is: UNDpay'
Copy the code

Let&const Temporary dead zone TDC

Let&const declared variables must precede use

let web = "pikasu1";
funciton func(){
consloe.log(web);
let web = "pikasu";
}
// An error will be reported
Copy the code
let web = "pikasu1";
funciton func(){
let web = "pikasu";// Correct: place it before use
consloe.log(web);
}
Copy the code

The common denominator of var-let-const

var web ="pikasu"/ / public
function show(){
var web = picazu/ / private
consloe.log(web);// Output function defined (picassu)
}
show();
consloe.log(web);// Output public (pikasu)
// Declare web inside a function as a private variable in the block scope
// The output is pikasu
//var-let-const is declared in the same way
Copy the code

Global pollution

web =picazu// Do not declare easy to pollute the global
consloe.log(web);
Copy the code
// Solution: Enable strict mode
"use strict";
web =picazu
consloe.log(web);// The output is wrong
Copy the code

Block scope Var has no block scope

var i = 99;// The global variable I is affected by the I in the for loop below
for(var i = 0; i<5; i++){ consloe.log(i); } consloe.log(i);// Output is 5
Copy the code
var i = 99;// Change var for to let or const
for(let i = 0; i<5; i++){ consloe.log(i); } consloe.log(i);// The output is 99
Copy the code

Const constant declaration to find out

const web = "pikasu";
web = "111";
// An error is reported because it cannot be changed in the same scope
Copy the code

Window global objects pollute and duplicate declarations

var screenLeft = 88;
console.log(window.screenLeft);
// The value of the window object is set to 88
Copy the code
// Change to let to declare variables
let screenLeft = 88;
console.log(window.screenLeft);// This outputs the value of the window object (the distance from the browser to the left).
console.log(screenLeft);// Here is the output 88
Copy the code

Object.freeze Freezes variables

const Host={
url:"https://www.jpymw.cn/".// Use semicolons because they are variables
port:443
};
Object.freeze(HOST);// Use object. freeze to freeze variables.
HOST.port=80;
console.log(HOST);// The output is 443

Copy the code

The value and addressing characteristics of scalar and reference types

Base quantity passing value, object passing value base quantity is making a copy of memory and objects refer to the same block of memory, and if they change they change together

Null and undefined

No value is undefined

Strict mode high quality code guards use Strict’s scope is when it goes down. For example, if the function is inside the function, it does not affect the outside function. If the function is inside the function, it also affects the strict mode

Assignment operators and arithmetic operators

let a=1,
b= 2;// The assignment operator assigns 2 to b

a += 5;// Arithmetic operator a = a + 5

Copy the code

Pre – and post-operations of unary operators

let n = 1;
++n; / / pre -
n++;// n = n+1;

let f = 2;
let d = f + ++n; // If n++ is added, then f + n++ is (+1).
consle.log(d);// f +n = 4; // f +n = 4; // f +n = 4;
//n++ is 3

Copy the code