ES6里有6种简单数据类型:undefined, null, Number, Boolean, String, Symbol
1种复杂数据类型:Object

typeof 操作符

image.png

undefined

未初始化的变量为undefined,永远不要显示的指定一个变量值为undefined
undefined值用于比较

  1. let xxx
  2. console.log(xxx == undefined) // true

typeof 一个未声明过的变量返回的是undefined

  1. let xxx
  2. typeof xxx
  3. "undefined"
  4. typeof ssssss
  5. "undefined"

null

Logically, a null value is an empty object pointer
如果要声明一个对象,但不打算赋值,就应该指定为null

  1. let car = null;
  2. console.log(typeof car); // "object"
  1. console.log(null == undefined); // true
  2. console.log(null === undefined); // false

Boolean

Boolean()函数可以将任何值转为true或false
5个falsy值:
0,NaN, “”, null, undefined

Number

decimal number 十进制

octal (base 8) 八进制以0开头

  1. let octalNum1 = 070; // octal for 56
  2. let octalNum2 = 079; // invalid octal - interpreted as 79
  3. let octalNum3 = 08; // invalid octal - interpreted as 8

hexadecimal (base 16) 十六进制以0x开头

  1. let hexNum1 = 0xA; // hexadecimal for 10
  2. let hexNum2 = 0x1f; // hexadecimal for 31

positive zero (+0) and negative zero (–0)

浮点数比整数占用双倍内存,JS总是想办法把数值转为整数

浮点数计算不精确

  1. if (0.1 + 0.2 == 0.3) { // avoid!
  2. console.log("You got 0.3.");
  3. }

数值范围

  1. Number.MIN_VALUE
  2. 5e-324
  3. Number.MAX_VALUE
  4. 1.7976931348623157e+308
  5. Infinity // (negative infinity)
  6. Infinity // (positive infinity)
  7. isFinite(-Infinity) // 判断是否介于[MIN_VALUE, MAX_VALUE]
  8. // false

NaN

  1. // 任何不能转换为number的值,isNaN都返回true
  2. console.log(isNaN(NaN)); // true
  3. console.log(isNaN(10)); // false - 10 is a number
  4. console.log(isNaN("10")); // false - can be converted to number 10
  5. console.log(isNaN("blue")); // true - cannot be converted to a number
  6. console.log(isNaN(true)); // false - can be converted to number 1

转换为number

  1. Number("hello") // NaN
  2. Number("123") // 123
  3. Number(true) // 1
  4. Number(null) // 0
  5. Number(undefined) // NaN
  6. // 将字符串转换为数值
  7. parseInt("0xf") // 15
  8. parseInt("AF", 16); // 175 指定基数radix
  9. parseInt("AF"); // NaN 默认10进制
  10. // parseFloat只能转换10进制,不能指定基数
  11. parseFloat("123") // 123
  12. parseFloat("0xAF") // 0
  13. parseFloat("1.22.33") // 1.22

String

toString()
可以将除null、undefined之外的任何值转为字符串
将数字转为字符串时可指定基数

  1. 0xAF.toString()
  2. "175"
  3. 0xAF.toString(16)
  4. "af"

String()可以将任何值转为字符串,有toString方法的调用toString方法

  1. String(0xAF)
  2. String(null)
  3. String(undefined)

+””可以转换字符串

  1. 1 + ""
  2. null + ""

文字模板template literals
用反引号backticks

  1. let myMultiLineTemplateLiteral = `first line
  2. second line`;

Interpolation插值替换

在``中用${}

  1. let name = "jack"
  2. let age = 19
  3. console.log(`Hi,${name}, you're ${age} years old`)

tag function

第一个参数strings是去除了${}后分隔的片段组成的数组
剩下的参数是${}里的值
用途:可以把${ a } + ${ b } = ${ a + b }里的${}值作为参数分解出来

  1. let a = 6;
  2. let b = 9;
  3. function simpleTag(strings, aValExpression, bValExpression, sumExpression) {
  4. console.log(strings); // 第一个参数传入的肯定是数组
  5. console.log(aValExpression);
  6. console.log(bValExpression);
  7. console.log(sumExpression);
  8. return 'foobar';
  9. }
  10. // 调用标签函数
  11. simpleTag`${ a } + ${ b } = ${ a + b }`
  12. // 结果
  13. ["", " + ", " = ", "", raw: Array(4)]
  14. 6
  15. 9
  16. 15
  17. "foobar"

用spread operator改写

  1. let a = 6;
  2. let b = 9;
  3. function simpleTag(strings, ...expressions) {
  4. console.log(strings); // 第一个参数传入的肯定是数组
  5. for(const expression of expressions){
  6. console.log(expression)
  7. }
  8. return 'foobar';
  9. }
  10. // 调用标签函数
  11. simpleTag`${ a } + ${ b } = ${ a + b }`

String.raw

  1. console.log(`\u00A9`); // ©
  2. console.log(String.raw`\u00A9`); // \u00A9

Symbol

symbol是唯一且不可变的,用于对象属性
symbols are intended to be used as unique tokens

  1. let genericSymbol = Symbol();
  2. console.log(genericSymbol); // Symbol()
  3. let fooSymbol = Symbol('foo');
  4. console.log(fooSymbol); // Symbol(foo);

Symbol()不能用new操作符

  1. let myBoolean = new Boolean();
  2. console.log(typeof myBoolean); // "object"
  3. let myString = new String();
  4. console.log(typeof myString); // "object"
  5. let myNumber = new Number();
  6. console.log(typeof myNumber); // "object"
  7. let mySymbol = new Symbol(); // TypeError: Symbol is not a constructor

Object

  1. let o = new Object();
  2. o.__proto__ === Object.prototype
  3. // 构造函数都有prototype

o.proto里的属性

  • constructor —The function that was used to create the object. In the previous example, the

constructor is the Object() function.

  • hasOwnProperty(propertyName) —Indicates if the given property exists on the object

instance (not on the prototype). The property name must be specified as a string (for
example, o.hasOwnProperty(“name”) ).