# 章序号/节序号/节/笔记序号 codecademy, Condition # 概念阐释 - 因为js属于**弱类型**语言,值的数据类型是可以改变的,比如`'1'` 会被转换为数值`1`。 - 所以因为js存在**强类型转换**,所以js中的**每个值**都可以被当作`true `或 `false `处理。*这个概念非常重要,这也是为何[[js short-circuit 短路值|短路值]]可以计算的原因。* - 大多数情况下用到的condition都是不为0的数字和布尔值,剩下的**只是为了检查变量是否有被赋值。** - **假值**可以被当作数字`0` - `0` 数字为0时 - Empty strings like `""` or `''` 引号内为空时 - `null` which represent when there is no value at all 当没有值 - `undefined` which represent when a declared variable lacks a value 当变量缺少值 - `NaN`, or Not a Number 不是数字 - **真值**可以被当作数字`1` - true 传统的布尔值 - 5 非0的数字 - 10/5 使用力数学运算符的 - `'true''carrot''0''false'`有内容的字符串 # 举例子 ![](http://image.harryrou.wiki/2023-01-12-%E6%88%AA%E5%B1%8F2023-01-12%2008.33.06.png) - numberOfApples的值为0, 数字0为假值,所以这里print `else`中的内容。 ![](http://image.harryrou.wiki/2023-01-12-%E6%88%AA%E5%B1%8F2023-01-12%2008.32.50.png) - myVariable的值是有内容的字符串,所以是真值,print `if`中的内容。 # 类比、比较与对比 # 问题 **1.** - 阐述什么是真值和假值? - 为什么js的每个值都可以被当作真值或假值? - 都有哪些情况下是假值,会被判断为false?哪些情况是真值,会被判定为true? - 通常这种真值或假值的概念是用来做什么的? **2.** 说出下方代码会print什么结果,并通过改变变量值来print另一个结果 ```js let wordCount = 0; if (wordCount) { console.log("Great! You've started your work!"); } else { console.log('Better get to work!'); } let favoritePhrase = 'Hello world'; if (favoritePhrase) { console.log("This string doesn't seem to be empty."); } else { console.log('This string is definitely empty.'); } ``` # 问题答案 ```js Output: Better get to work! This string doesn't seem to be empty. ``` # 备注(经验集/错误集)