美文网首页
JS 逻辑运算符

JS 逻辑运算符

作者: coderhzc | 来源:发表于2021-12-06 14:57 被阅读0次

typeof 打印出来的都是字符串

逻辑或 ||

业务场景: 当在一个函数中有一个参数传递过来,做判断你可以不使用if.....else

// 适用if else
function sun( name) {
  if(!name) {
    return "你还没有写姓名!"
  }
  return name
}

console.log(sun())
console.log(sun("huzhenchu"))
/********以下精简***********/

function sun(name) {
  return name || "你还没有写姓名!"
}
console.log(sun())
console.log(sun("huzhenchu"))


// 默认值的基本写法
function test(a ,  b ) {
  var a = arguments[0] || 1;
  var b = arguments[1] || 2
  return console.log(a + b ) 
}

// test调用输出结果
test() // 3
test(4, 5) // 9

实际截图

image.png

typeof 打印出来的都是字符串

 function test(a, b) {
      a = typeof (arguments[0]) !== undefined ? arguments[0] : 1;
      b = typeof (arguments[1]) !== undefined ? arguments[1] : 2;
      return console.log(a + b)
    }
    test(4,5)

实际截图:

image.png

相关文章

网友评论

      本文标题:JS 逻辑运算符

      本文链接:https://www.haomeiwen.com/subject/buogxrtx.html