首页 > 解决方案 > 我不明白这段代码谁能解释:

问题描述

我无法理解这段代码,任何人都可以解释:

const statDateMatch = typeof startDate !== 'number' || expense.createdAt >= startDate;

谢谢。

标签: javascript

解决方案


由于我们使用的是 OR 运算符||,因此我们说如果任何一个条件为真,则为startDateMatch真,否则为假。

因此,在 OR 的左侧,我们typeof startDate !== 'number'询问 ... 是变量的类型startDate不等于字符串number。让我们调用这个A的结果。

在右边,我们expense.createdAt >= startDate问 ... 变量的值是否expense.createdAt大于或等于 的值startDate。我们将其结果称为B

所以最后我们要说的是,如果 A 或 B 的任何一个结果为真,则将其分配给 constant startDateMatch。如果它们都不是正确的,那么startDateMatch将是错误的。

const startDateMatch is equal to: Is any one of the results from A and B true?

说得通?


推荐阅读