doc: add notes about declaring types in javascript section

This commit is contained in:
Leon Xu committed 2021-08-26 17:01:17 -07:00
1 parent 9689e4cb1b
commit 0472fa7065
1 file changed
+13 -1
+13 -1
View File
@@ -79,4 +79,16 @@ let temps = [55, 60, 57.5]; // Array
JavaScript的类型是没有指定类型,但是JS有类型(“松散类型”)。
- Number, Boolean, String, Array, Object, Function, Null, Undefined
- 可以通过调用[typeof](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)来确定变量的类型,但通常这是一个糟糕的方法
- 注意:类型转换[并不总是您所期望的](https://stackoverflow.com/questions/7202157/why-does-return-the-string-10)
- 注意:类型转换[并不总是您所期望的](https://stackoverflow.com/questions/7202157/why-does-return-the-string-10)
## 关于在JavaScript中声明类型的注意事项
如果您使用静态类型语言(如Java)编程,您会记得在声明变量时,必须指定它们的类型,并且必须始终保持相同。
```javascript
boolean isValid = "hello!"; //错误
```
在像JavaScript这样的动态类型语言中,你不需要指定类型(只需要使用let或const),你可以在以后的执行中改变变量所引用的类型。
```javascript
let isValid = true; //没有错误
isValid = "hello!";
isValid = 1;
```