JavaScript中的逻辑运算符 - 您如何使用它们? [英] Logical operators in JavaScript — how do you use them?

查看:135
本文介绍了JavaScript中的逻辑运算符 - 您如何使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白&& || 的方式!工作......包括bool和其他数据类型。你如何使用它们?

I don't understand how &&, ||, and ! work... both with bools and other data types. How do you use them?

推荐答案

Javascript中的所有值都是truthy或falsy。

All values in Javascript are either "truthy" or "falsy".


  • a&& b 评估第一个假操作数

  • a || b 计算到第一个真正的操作数

  • a && b evaluates to the first falsy operand
  • a || b evaluates to the first truthy operand

两个运算符都不会在返回之后计算任何操作数。

如果所有操作数都不匹配,它将评估到最后一个。

Both operators will not evaluate any operands after the one the return.
If all operands don't match, it will evaluate to the last one.


  • !a 评估为 true 如果 a 是假的, false 如果 a 是真实的。

  • !a evaluates to true if a is falsy and false if a is truthy.

所有值都是真实的,除了以下, falsy

All values are truthy except the following, which are falsy:


  • false

  • +0

  • -0

  • NaN


  • null

  • undefined

  • document.all

  • false
  • +0
  • -0
  • NaN
  • ""
  • null
  • undefined
  • document.all

这篇关于JavaScript中的逻辑运算符 - 您如何使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆