New JavaScript syntax “double question mark syntax” and “optional chain syntax”


Double question mark grammar

Double question mark grammar
The concept is to return the value on the right when the value on the left is null or undefined

let Form = undefined ?? true;   //Form = true

Alternative chain syntax

? optional chain

//Optional chain
let body = {
    value: {
        a: '123321'
let flag = body ? body.value : undefined
//Equivalent to
let result = body?.value

let select = {
    value: {
        a: null
let entirely = select?.value?.a || 100
//If select and select.value and select.value When. A are null or undefined, the entity is equal to 100
//If a = 0, entity must be equal to 100, which can only be written as follows

let over = select.value.a === null ? 100 : select?.value?.a

The code is stored in GitHub

This work adoptsCC agreementReprint must indicate the author and the link of this article