Summary of JavaScript knowledge points

Updated from time to time, only for notes

 

catalogue

1, Comparison of var, let and const

2, Cookie, localStorage, sessionStorage comparison

3, String connection

1. Use connector "+"

2. join method using array

3. Use the template string backquote ` 'of ES6

4. Using concat method

4, JS operator

1. Non air operator

2,?? Non air operator

3,??= Null assignment operator

4,?. Chain judgment operator

5,? : Ternary operator

1, Comparison of var, let and const

varletconst
ScopeGlobal scope, function scope, and {} will also be falsely called block scopeBlock level scopeBlock level scope
Variable promotionYes, but you will get the default value undefinedNo, 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 declarationssureIt is not allowed to declare the same variable repeatedly within the same scopeIt is not allowed to declare the same variable repeatedly within the same scope
Global objectGlobal 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 deleteA global variable declared with the const command is not a property of a global objectA global variable declared with the const command is not a property of a global object
variabilitysuresureCannot be assigned again
initializationNot requiredNot requiredessential

2, Cookie, localStorage, sessionStorage comparison

cookielocalStoragesessionStorage
life cycleIt 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 defaultSave permanently unless clearedValid only when starting a session, cleared after closing the page or browser
data sizeAbout 4KGenerally 5MB
Communication with serverIt will be carried in the HTTP header every time. If you use cookies to save multiple data, it will cause performance problemsIt is only saved in the customer service terminal and does not participate in the communication with the server
Ease of useThe native Cookie interface is unfriendly and needs to be encapsulated by itselfThe 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

Keywords: Javascript

Added by marty_arl on Tue, 18 Jan 2022 04:50:02 +0200