【ES6】运算符

46 阅读1分钟

一、指数运算符

    console.log(3 ** 2);//9
    console.log((-3) ** 2); //9
    console.log(5 ** 2);//25

二、链式运算符

三、Null判断符

读取对象属性的时候,如果某个属性的值是null或undefined,有时候需要为它们指定默认值。通过使用||运算符指定默认值。

    let n=null
    const a =n||"为Null"
    console.log(a);//为Null

由于||中使用null、undefined、false、0、空字符串都会使默认值生效果,使用??只有null和undefined才会生效。

let n=null
    const a =n??"为Null"
    const b=undefined??"为Undefined"
    const c=false??"为False"
    const d=0??"为0"
    console.log(a);//为Null
    console.log(b);//为Undefined
    console.log(c);//false
    console.log(d);//0

四、逻辑赋值运算符

let a="A"
     a=a||1
    // 等价于  a||=1