• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

凉梁凉糕

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

js 通过if变量判断时注意事项

1 注意 if内部的变量即使不等于true 也会走到内部 ,如在 console 打印下面的内容 扛具体的结果

'请输入' == true
 false
 if('请输入'){console.log(11111)}
 11111

即使用 if(myPars){执行命令 } 是判断 myPars 不是'' ,0 undefined null 则就不会执行内部的命令, 如下面的内容;

      console.log('===============')
      let t = 'test'
      let tt = null
      let ttt
      let tttt = ''
      let ttttt = 0
      let tttttt = 2
      if (t) {
        console.log('t')
      }
      if (tt) {
        console.log('tt')
      }
      if (ttt) {
        console.log('ttt')
      }
      if (tttt) {
        console.log('tttt')
      }
      if (ttttt) {
        console.log('ttttt')
      }
      if (tttttt) {
        console.log('tttttt')
      }
      console.log('==================')

打印的结果将是 :

==================
 t
 tttttt
==================

  

posted on 2019-11-04 16:32  凉梁凉糕  阅读(675)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3