2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
今天这篇文章,我们详细讲一下基本数据类型。
|
|
|
|
|
|
|
|
|
|
## String 字符串
|
|
|
|
|
|
2019-12-12 11:57:56 +08:00
|
|
|
|
### 语法
|
|
|
|
|
|
|
|
|
|
字符串型可以是引号中的任意文本,其语法为:双引号 `""` 或者单引号 `''`。
|
|
|
|
|
|
|
|
|
|
来看个示例。下面的这些,都是字符串:
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var a = "abcde";
|
2019-12-12 11:57:56 +08:00
|
|
|
|
var b = "千古壹号";
|
2019-12-11 19:59:12 +08:00
|
|
|
|
var c = "123123";
|
2019-12-12 11:57:56 +08:00
|
|
|
|
var d = '哈哈哈哈哈';
|
2019-12-11 19:59:12 +08:00
|
|
|
|
var e = ""; //空字符串
|
|
|
|
|
|
2019-12-12 11:57:56 +08:00
|
|
|
|
var f = haha; // 没使用引号,到这里会直接报错。因为会被认为是js代码,但是之前并没有定义 haha。
|
|
|
|
|
|
2019-12-11 19:59:12 +08:00
|
|
|
|
console.log(typeof a);
|
|
|
|
|
console.log(typeof b);
|
|
|
|
|
console.log(typeof c);
|
|
|
|
|
console.log(typeof d);
|
|
|
|
|
console.log(typeof e);
|
2019-12-12 11:57:56 +08:00
|
|
|
|
|
2019-12-11 19:59:12 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
控制台输出如下:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
string
|
|
|
|
|
string
|
|
|
|
|
string
|
|
|
|
|
string
|
|
|
|
|
string
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-12 11:57:56 +08:00
|
|
|
|
### 引号的注意事项
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 11:57:56 +08:00
|
|
|
|
1、单引号和双引号不能混用。比如下面这样写是不可以的:
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
```javascript
|
2019-12-12 11:57:56 +08:00
|
|
|
|
var str = 'hello"; // 报错:Uncaught SyntaxError: Invalid or unexpected token
|
2019-12-11 19:59:12 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-12-12 11:57:56 +08:00
|
|
|
|
2、同类引号不能嵌套:双引号里不能再放双引号,单引号里不能再放单引号。
|
|
|
|
|
|
|
|
|
|
3、单引号里可以嵌套双引号;双引号里可以嵌套单引号。
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 11:57:56 +08:00
|
|
|
|
### 转义字符
|
|
|
|
|
|
|
|
|
|
在字符串中我们可以使用`\`作为转义字符,当表示一些特殊符号时可以使用`\`进行转义。
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
|
2020-06-06 23:50:36 +08:00
|
|
|
|
- `\"` 表示 `"` 双引号
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2020-06-06 23:50:36 +08:00
|
|
|
|
- `\'` 表示 `'` 单引号
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 11:57:56 +08:00
|
|
|
|
- `\\` 表示`\`
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
- `\r` 表示回车
|
|
|
|
|
|
2019-12-12 11:57:56 +08:00
|
|
|
|
- `\n` 表示换行。n 的意思是 newline。
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 11:57:56 +08:00
|
|
|
|
- `\t` 表示缩进。t 的意思是 tab。
|
|
|
|
|
|
|
|
|
|
- `\b` 表示空格。b 的意思是 blank。
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var str1 = "我说:\"今天\t天气真不错!\"";
|
|
|
|
|
var str2 = "\\\\\\";
|
|
|
|
|
|
|
|
|
|
console.log(str1);
|
|
|
|
|
console.log(str2);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
上方代码的打印结果:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
我说:"今天 天气真不错!"
|
|
|
|
|
\\\
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-12 11:57:56 +08:00
|
|
|
|
### 获取字符串的长度
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 11:57:56 +08:00
|
|
|
|
字符串是由若干个字符组成的,这些字符的数量就是字符串的长度。我们可以通过字符串的 length 属性可以获取整个字符串的长度。
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 11:57:56 +08:00
|
|
|
|
代码举例:
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 11:57:56 +08:00
|
|
|
|
```javascript
|
|
|
|
|
var str1 = '千古壹号';
|
|
|
|
|
var str2 = '千古壹号,永不止步!';
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 11:57:56 +08:00
|
|
|
|
var str3 = 'qianguyihao';
|
|
|
|
|
var str4 = 'qianguyihao, keep moving!';
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 11:57:56 +08:00
|
|
|
|
console.log(str1.length); // 4
|
|
|
|
|
console.log(str2.length); // 10
|
|
|
|
|
console.log(str3.length); // 11
|
|
|
|
|
console.log(str4.length); // 25
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
由此可见,字符串的 length 属性,在判断字符串的长度时,会认为:
|
2019-12-12 11:57:56 +08:00
|
|
|
|
|
|
|
|
|
- 一个中文算一个字符,一个英文算一个字符
|
|
|
|
|
|
|
|
|
|
- 一个标点符号(包括中文标点、英文标点)算一个字符
|
|
|
|
|
|
|
|
|
|
- 一个空格算一个字符
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 11:57:56 +08:00
|
|
|
|
### 字符串拼接
|
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
多个字符串之间可以使用加号 `+` 进行拼接。
|
2019-12-12 11:57:56 +08:00
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
**拼接语法**:
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
```
|
2019-12-12 15:04:44 +08:00
|
|
|
|
字符串 + 任意数据类型 = 拼接之后的新字符串;
|
2019-12-11 19:59:12 +08:00
|
|
|
|
```
|
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
**拼接规则**:拼接前,会把与字符串相加的这个数据类型转成字符串,然后再拼接成一个新的字符串。
|
|
|
|
|
|
|
|
|
|
**代码举例**:(字符串与六大数据类型相加)
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var str1 = '千古壹号' + '永不止步';
|
|
|
|
|
var str2 = '千古壹号' + 666;
|
|
|
|
|
var str3 = '千古壹号' + true;
|
|
|
|
|
var str4 = '千古壹号' + null;
|
|
|
|
|
var str5 = '千古壹号' + undefined;
|
|
|
|
|
|
|
|
|
|
var obj = { name: '千古壹号', age: 28 };
|
|
|
|
|
var str6 = '千古壹号' + obj;
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
console.log(str1);
|
|
|
|
|
console.log(str2);
|
|
|
|
|
console.log(str3);
|
|
|
|
|
console.log(str4);
|
|
|
|
|
console.log(str5);
|
|
|
|
|
console.log(str6);
|
|
|
|
|
```
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
打印结果:
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
```
|
|
|
|
|
千古壹号永不止步
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
千古壹号666
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
千古壹号true
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
千古壹号null
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
千古壹号undefined
|
|
|
|
|
|
|
|
|
|
千古壹号[object Object]
|
|
|
|
|
```
|
2019-12-12 11:57:56 +08:00
|
|
|
|
|
2020-06-06 23:50:36 +08:00
|
|
|
|
## 模板字面量(模板字符串)
|
|
|
|
|
|
|
|
|
|
ES6中引入了**模板字面量**,让我们省去了字符串拼接的烦恼。
|
|
|
|
|
|
|
|
|
|
### 在模板字符串中插入变量
|
|
|
|
|
|
|
|
|
|
举例:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
let name = 'qianguyihao';
|
|
|
|
|
|
|
|
|
|
// 下面这一行是模板字面量,注意语法格式
|
|
|
|
|
console.log(`我是${name}`); // 打印结果:我是 qianguyihao
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 在模板字面量中插入表达式
|
|
|
|
|
|
|
|
|
|
在字符串中插入表达式,以往的写法必须是这样的:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const a = 5;
|
|
|
|
|
const b = 10;
|
|
|
|
|
console.log('this is ' + (a + b) + ' and\nnot ' + (2 * a + b) + '.');
|
|
|
|
|
```
|
|
|
|
|
现在通过模板字面量,我们可以使用一种更优雅的方式来表示:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const a = 5;
|
|
|
|
|
const b = 10;
|
|
|
|
|
|
|
|
|
|
console.log(`this is ${a + b} and
|
|
|
|
|
not ${2 * a + b}.`);
|
2020-06-07 21:58:03 +08:00
|
|
|
|
```
|
|
|
|
|
打印结果:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
this is 15 and
|
|
|
|
|
not 20.
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 换行时不容易出错
|
|
|
|
|
|
|
|
|
|
上面的例子中,也可以看出这个特征。
|
|
|
|
|
|
|
|
|
|
### 在模板字面量中插入函数返回值
|
|
|
|
|
|
|
|
|
|
举例:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function getName() {
|
|
|
|
|
return 'qianguyihao';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(`www.${getName()}.com`); // 打印结果:www.qianguyihao.com
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 模板字面量支持嵌套使用
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const nameList = ['千古壹号', '许嵩', '解忧少帅'];
|
|
|
|
|
|
|
|
|
|
function myTemplate() {
|
|
|
|
|
// join('') 的意思是,把数组里的内容合并成一个字符串
|
|
|
|
|
return `<ul>
|
|
|
|
|
${nameList
|
|
|
|
|
.map((item) => `<li>${item}</li>`)
|
|
|
|
|
.join('')}
|
|
|
|
|
</ul>`;
|
|
|
|
|
}
|
|
|
|
|
document.body.innerHTML = myTemplate();
|
2020-06-06 23:50:36 +08:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2020-06-07 21:58:03 +08:00
|
|
|
|
效果如下:
|
2020-06-06 23:50:36 +08:00
|
|
|
|
|
2020-06-07 21:58:03 +08:00
|
|
|
|
![](http://img.smyhvae.com/20200607_2118.png)
|
2020-06-06 23:50:36 +08:00
|
|
|
|
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
## 数值型:Number
|
|
|
|
|
|
2019-12-12 15:51:08 +08:00
|
|
|
|
在JS中所有的数值都是 Number 类型,包括整数和浮点数(小数)。
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
```javascript
|
2019-12-12 15:04:44 +08:00
|
|
|
|
var a = 100; // 定义一个变量 a,并且赋值整数100
|
|
|
|
|
console.log(typeof a); // 输出变量 a 的类型
|
|
|
|
|
|
|
|
|
|
var b = 12.3; // 定义一个变量 b,并且赋值浮点数 12.3
|
|
|
|
|
console.log(typeof a);
|
2019-12-11 19:59:12 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
上方代码的输出结果为:
|
|
|
|
|
|
|
|
|
|
```
|
2019-12-12 15:04:44 +08:00
|
|
|
|
number
|
|
|
|
|
|
2019-12-11 19:59:12 +08:00
|
|
|
|
number
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-12 15:51:08 +08:00
|
|
|
|
再次补充:在 JS 中,只要是数,就是 Number 数值型的。无论整浮、浮点数(即小数)、无论大小、无论正负,都是 Number 类型的。
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
### 数值范围
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
由于内存的限制,ECMAScript 并不能保存世界上所有的数值。
|
|
|
|
|
|
|
|
|
|
- 最大值:`Number.MAX_VALUE`,这个值为: 1.7976931348623157e+308
|
|
|
|
|
|
|
|
|
|
- 最小值:`Number.MIN_VALUE`,这个值为: 5e-324
|
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
如果使用 Number 表示的变量超过了最大值,则会返回Infinity。
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
- 无穷大(正无穷):Infinity
|
|
|
|
|
|
|
|
|
|
- 无穷小(负无穷):-Infinity
|
|
|
|
|
|
|
|
|
|
注意:`typeof Infinity`的返回结果是number。
|
|
|
|
|
|
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
### NaN
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
**NaN**:是一个特殊的数字,表示Not a Number,非数值。比如:
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
console.log("abc" / 18); //结果是NaN
|
|
|
|
|
|
|
|
|
|
console.log("abc" * "abcd"); //按理说,字符串相乘是没有结果的,但如果你非要让JS去算,它就一定会给你一个结果。结果是NaN
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
注意:`typeof NaN`的返回结果是 number。
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
Undefined和任何数值计算的结果为 NaN。NaN 与任何值都不相等,包括 NaN 本身。
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
另外,关于 isNaN() 函数,可以看下一篇文章。
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
### 浮点数的运算
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
在JS中,整数的运算**基本**可以保证精确;但是**小数的运算,可能会得到一个不精确的结果**。所以,千万不要使用JS进行对精确度要求比较高的运算。
|
|
|
|
|
|
|
|
|
|
如下:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var a = 0.1 + 0.2;
|
|
|
|
|
console.log(a); //打印结果:0.30000000000000004
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
上方代码中,打印结果并不是0.3,而是0.30000000000000004。
|
|
|
|
|
|
|
|
|
|
我们知道,所有的运算都要转换成二进制去计算,然而,二进制是无法精确表示1/10的。因此存在小数的计算不精确的问题。
|
|
|
|
|
|
|
|
|
|
### 连字符和加号的区别
|
|
|
|
|
|
|
|
|
|
键盘上的`+`可能是连字符,也可能是数字的加号。如下:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
console.log("我" + "爱" + "你"); //连字符,把三个独立的汉字,连接在一起了
|
|
|
|
|
console.log("我+爱+你"); //原样输出
|
|
|
|
|
console.log(1+2+3); //输出6
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
输出:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
我爱你
|
|
|
|
|
我+爱+你
|
|
|
|
|
6
|
|
|
|
|
```
|
|
|
|
|
|
2020-06-06 17:15:17 +08:00
|
|
|
|
**总结**:如果加号两边**都是** Number 类型,此时是数字相加。否则,就是连字符(用来连接字符串)。
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
举例1:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var a = "1";
|
|
|
|
|
var b = 2;
|
|
|
|
|
console.log(a + b);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
控制台输出:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
12
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
举例2:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
var a = 1;
|
|
|
|
|
var b = 2;
|
|
|
|
|
console.log("a" + b); //"a"就不是变量了!所以就是"a"+2 输出a2
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
控制台输出:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
a2
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
于是我们明白了,在变量中加入字符串进行拼接,可以被同化为字符串。【重要】
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### 隐式转换
|
|
|
|
|
|
|
|
|
|
我们知道,`"2"+1`得到的结果其实是字符串,但是`"2"-1`得到的结果却是数值1,这是因为计算机自动帮我们进行了“**隐式转换**”。
|
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
也就是说,`-`、`*`、`/`、`%`这几个符号会自动进行隐式转换。例如:
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var a = "4" + 3 - 6;
|
|
|
|
|
console.log(a);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
输出结果:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
37
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
虽然程序可以对`-`、`*`、`/`、`%``这几个符号自动进行“隐式转换”;但作为程序员,我们最好自己完成转换,方便程序的可读性。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 布尔值:Boolean
|
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
布尔型有两个值:true 和 false。主要用来做逻辑判断: true 表示真,false 表示假。
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
布尔值直接使用就可以了,千万不要加上引号。
|
|
|
|
|
|
|
|
|
|
代码:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var a = true;
|
|
|
|
|
console.log(typeof a);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
控制台输出结果:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
boolean
|
|
|
|
|
```
|
|
|
|
|
|
2019-12-12 15:04:44 +08:00
|
|
|
|
布尔型和数字型相加时, true 按 1 来算 ,false 按 0 来算。
|
|
|
|
|
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
## 变量值的传递(赋值)
|
|
|
|
|
|
|
|
|
|
语句:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
a = b;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
把b的值赋给a,b不变。
|
|
|
|
|
|
|
|
|
|
将等号右边的值,赋给左边的变量;等号右边的变量,值不变。
|
|
|
|
|
|
|
|
|
|
来做几个题目。
|
|
|
|
|
|
|
|
|
|
举例1:
|
|
|
|
|
|
2020-05-15 20:05:56 +08:00
|
|
|
|
```js
|
2019-12-11 19:59:12 +08:00
|
|
|
|
//a b c
|
|
|
|
|
var a = 1; //1
|
|
|
|
|
var b = 2; //1 2
|
|
|
|
|
var c = 3; //1 2 3
|
|
|
|
|
a = b + c; //5 2 3
|
|
|
|
|
b = c - a; //5 -2 3
|
|
|
|
|
c = a * b; //5 -2 -10
|
|
|
|
|
console.log(a);
|
|
|
|
|
console.log(b);
|
|
|
|
|
console.log(c);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
输出:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
5
|
|
|
|
|
-2
|
|
|
|
|
-10
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
举例2:
|
|
|
|
|
|
2020-05-15 20:05:56 +08:00
|
|
|
|
```js
|
2019-12-11 19:59:12 +08:00
|
|
|
|
//a b c
|
|
|
|
|
var a = 1;
|
|
|
|
|
var b = 2;
|
|
|
|
|
var c = 3; //1 2 3
|
|
|
|
|
a = a + b; //3 2 3
|
|
|
|
|
b = b + a; //3 5 3
|
|
|
|
|
c = c + b; //3 5 8
|
|
|
|
|
console.log(a); //3
|
|
|
|
|
console.log(b); //5
|
|
|
|
|
console.log(c); //8
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
输出:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
3
|
|
|
|
|
5
|
|
|
|
|
8
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
举例3:
|
|
|
|
|
|
2020-05-15 20:05:56 +08:00
|
|
|
|
```js
|
2019-12-11 19:59:12 +08:00
|
|
|
|
//a b
|
|
|
|
|
var a = "1";
|
|
|
|
|
var b = 2; //"1" 2
|
|
|
|
|
a = a + b; //"12" 2
|
|
|
|
|
b = b + a; //"12" "212"
|
|
|
|
|
console.log(a); //输出12
|
|
|
|
|
console.log(b); //输出212
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
输出:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
12
|
|
|
|
|
212
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
举例4:
|
|
|
|
|
|
2020-05-15 20:05:56 +08:00
|
|
|
|
```js
|
2019-12-11 19:59:12 +08:00
|
|
|
|
//a b
|
|
|
|
|
var a = "1";
|
|
|
|
|
var b = 2;
|
|
|
|
|
a = b + a; //"21" 2
|
|
|
|
|
b = b + a; //"21" "221"
|
|
|
|
|
console.log(a); //21
|
|
|
|
|
console.log(b) //221
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
效果:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
21
|
|
|
|
|
221
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
举例5:(这个例子比较特殊,字符串减去数字)
|
|
|
|
|
|
2020-05-15 20:05:56 +08:00
|
|
|
|
```js
|
2019-12-11 19:59:12 +08:00
|
|
|
|
var a = "3";
|
|
|
|
|
var b = 2;
|
|
|
|
|
console.log(a-b);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
效果:(注意,字符串 - 数值 = 数值)
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
1
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 我的公众号
|
|
|
|
|
|
2020-06-06 17:15:17 +08:00
|
|
|
|
想学习**代码之外的技能**?不妨关注我的微信公众号:**千古壹号**。
|
2019-12-11 19:59:12 +08:00
|
|
|
|
|
|
|
|
|
扫一扫,你将发现另一个全新的世界,而这将是一场美丽的意外:
|
|
|
|
|
|
|
|
|
|
![](http://img.smyhvae.com/20200101.png)
|
|
|
|
|
|