Updated from time to time, only for notes
catalogue
1, Comparison of var, let and const
2, Cookie, localStorage, sessionStorage comparison
3. Use the template string backquote ` 'of ES6
3,??= Null assignment operator
1, Comparison of var, let and const
var | let | const | |
---|---|---|---|
Scope | Global scope, function scope, and {} will also be falsely called block scope | Block level scope | Block level scope |
Variable promotion | Yes, but you will get the default value undefined | No, ReferenceError will be prompted before declaration, which is called "temporary dead zone" (TDZ for short) | No, ReferenceError will be prompted before declaration, which is called "temporary dead zone" (TDZ for short) |
Multiple declarations | sure | It is not allowed to declare the same variable repeatedly within the same scope | It is not allowed to declare the same variable repeatedly within the same scope |
Global object | Global variables declared with var and function are still the attributes of global objects. Global variables declared with var cannot be deleted from window/global (Global is for node environment) with delete | A global variable declared with the const command is not a property of a global object | A global variable declared with the const command is not a property of a global object |
variability | sure | sure | Cannot be assigned again |
initialization | Not required | Not required | essential |
2, Cookie, localStorage, sessionStorage comparison
cookie | localStorage | sessionStorage | |
life cycle | It is generally generated by the server, and the expiration time can be set. If a Cookie is generated on the browser side, it will expire after closing the browser by default | Save permanently unless cleared | Valid only when starting a session, cleared after closing the page or browser |
data size | About 4K | Generally 5MB | |
Communication with server | It will be carried in the HTTP header every time. If you use cookies to save multiple data, it will cause performance problems | It is only saved in the customer service terminal and does not participate in the communication with the server | |
Ease of use | The native Cookie interface is unfriendly and needs to be encapsulated by itself | The native interface is acceptable, and can also be encapsulated again to better support Object and Array |
3, String connection
1. Use connector "+"
let a = 'java' let b = a + 'script' // javascript
2. join method using array
let arr = ['hello', 'java', 'script'] let str = arr.join("") // hellojavascript
3. Use the template string backquote ` 'of ES6
let a = 'java' let b = `hello ${a}script` // hello javascript
4. Using concat method
var a = 'java' var b = 'script' var str = a.concat(b) // javascript
4, JS operator
1. Non air operator
function (obj) { let a = obj || {} } Equivalent to function (obj) { let a; if (obj === 0 || obj === '' || obj === false || obj === null || obj === undefined) { a = {} } else { a = obj } }
2,?? Non air operator
function (obj) { let a = obj ?? {} } Equivalent to function (obj) { let a; if (obj === null || obj === undefined) { a = {} } else { a = obj } }
3,??= Null assignment operator
This assignment operator assigns a value only if the value is null or undefined
let x = null let y = 5 console.log(x ??= y) // 5
4,?. Chain judgment operator
Allows you to read property values deeply nested in the object chain without having to validate each reference. When the reference is empty, the expression stops evaluating and returns undefined
let travelPlans = { destination: 'DC', monday: { location: 'National Mall, budget: 200 } } console.log(travelPlans.tuesday?.location) // undefined
5,? : Ternary operator
function checkCharge(charge) { return (charge > 0) ? 'yes' : 'no' } console.log(checkCharge(20)) // yes