zoukankan      html  css  js  c++  java
  • 1.20 逻辑运算符或(||)的测试

    当第一个变量a为falsy时,就返回第二个变量b(哪怕第二个变量b也是falsy)

    当第一个变量a为正确时,就返回第一个变量a(不计算第二个变量,哪怕第二个变量会产生报错也没关系(因为不进行计算))

    • 当undefined进行.操作时会立即报错
        var a 
        const c = a.z || 'xxx'  //a.z即为undefined.z,报错
        console.log(c);
      //TypeError: Cannot read property 'z' of undefined
    
        var a
        const c = 'xxx' || a.z  //当左边操作数为正确时,就返回左边操作数(不计算右边操作数,哪怕右边操作数会产生报错也没关系(因为不进行计算))
        console.log(c);// 'xxx'
    

    • 当逻辑运算符||前面的变量类型为undefined或null或""和false、0这类falsy值时,会返回后面的变量值。(??运算符只针对undefined或null)
        var a = ''
        const c = a.z || null  //undefined|| null
        console.log(c); //null
    
        const c = ''|| null
        console.log(c); //null
    
        const c = false|| null
        console.log(c); //null
    
        const c = null || undefined 
        console.log(c); //undefined
    
        const c = 0 || null
        console.log(c); // null
    
  • 相关阅读:
    react开发环境搭建
    react 组件创建
    Redux 视频教程
    echars3.0 柱状图y轴字体斜放
    echars3.0 柱状图大小设置
    ECharts地图详解 【转】
    html 超出出现省略号
    html JS打印添加水印图片
    js 重庆38区县 数组
    docker 暴露2375 端口。
  • 原文地址:https://www.cnblogs.com/xjt31/p/14305456.html
Copyright © 2011-2022 走看看