数据类型
一、有哪些数据类型
栈:基本类型:Undefined、Null、Boolean、Number、String
堆:引用类型:Array、Object、Function
Symbol和BigInt是ES6新增的数据类型:
Symbol:创建独一无二且不可变的数据类型,主要是为了解决可能出现的全局变量冲突的问题。
BigInt:是一种数字类型的数据,可以表示任意精度格式的整数。用于安全的存储和操作大数据。
两种类型的区别在于存储位置不同:
1、原始数据类型存放在栈中,占据空间小、大小固定,被频繁使用。
2、引用类型存储在堆中的对象,占据空间大、大小不固定,在栈中存储了指针,该指针指向堆中该实体的起始地址。
二、数据类型检测的方式有哪些
1、typeof:object、null、array类型的值都是object,其他类型的都是正确。
console.log(typeof 2); // number
console.log(typeof true); // boolean
console.log(typeof 'str'); // string
console.log(typeof []); // object
console.log(typeof function(){
}); // function
console.log(typeof {
}); // object
console.log(typeof undefined); // undefined
console.log(typeof null); // object
2、instanceof:其内部运行机制是判断在其原型链中能否找到该类型的原型。(不能检测基本类型,只能检测引用类型)
//基本类型无法检测
console.log(2 instanceof Number); // false
console.log(true instanceof Boolean); // false
console.log('str' instanceof String); // false
//引用类型可以检测
console.log([] instanceof Array); // true
console.log(function(){
} instanceof Function); // true
console.log({
} instanceof Object); // true
3、constructor
console.log((2).constructor === Number); // true
console.log((true).constructor === Boolean); // true
console.log(('str').constructor === String); // true
console.log(([]).constructor === Array); // true
console.log((function() {
}).constructor === Function); // true
console.log(({
}).constructor === Object); // true
4、Object.prototype.toString.call()
var a = Object.prototype.toString;
console.log(a.call(2));//[object Number]
console.log(a.call(true));//[object Boolean]
console.log(a.call('str'));//[object String]
console.log(a.call([]));//[object Array]
console.log(a.call(function () {
}));//[object Function]
console.log(a.call({
}));//[object Object]
console.log(a.call(undefined));//[object Undefined]
console.log(a.call(null));//[object Null]
三、判断数组的方式有哪些
1、通过Object.prototype.toString.call()做判断
var arr = [];
console.log(Object.prototype.toString.call(arr).slice(8,-1) === 'Array');//true
2、通过原型链做判断
var arr = [112, 2]
console.log(arr.__proto__ === Array.prototype);//true
3、通过ES6的Array.isArray()做判断
var arr = [112, 2]
console.log(Array.isArray(arr));//true
4、通过instanceof做判断
var arr = [112, 2]
console.log(arr instanceof Array);//true
5、通过Array.prototype.isPrototypeOf
var arr = [112, 2]
console.log(Array.prototype.isPrototypeOf(arr));//true
四、null和undefined的区别
同:都是基本数据类型,都存放在栈中。
异:
1、含义:null是空对象,主要用于初始化;undefined是未定义,定义未赋值的变量,值为undefined。
2、保留字:null是保留字不可以作为变量名;undefined不是保留字,可以用作变量名,输出不会报错,但是会产生冲突。
3、typeof:typeof null === ‘object’;typeof undefined === ‘undefined’。
console.log(null == undefined);//true
console.log(null === undefined);//false
五、为什么0.1+0.2 ! == 0.3,如何让其相等?
console.log(0.1 + 0.2 === 0.3) // false 为什么呢?
因为计算机是通过二进制的方式存储数据的,所以计算0.1+0.2的时候,需要先转化为二进制再求和,这其中存在着一些精度误差,0.1+0.2=0.30000000000000004。
可以通过四舍五入进行判断
console.log((0.1+0.2).toFixed(2)==0.3);//true
六、typeof NaN 的结果是什么?
NaN 指“不是一个数字”(not a number),NaN 是一个“警戒值”(sentinel value,有特殊用途的常规值),用于指出数字类型中的错误情况,即“执行数学运算没有成功,这是失败后返回的结果”。
console.log(typeof NaN); // "number"
console.log(NaN !== NaN)
七、isNaN 和 Number.isNaN 函数的区别?
1、函数 isNaN 接收参数后,会尝试将这个参数转换为数值,任何不能被转换为数值的的值都会返回 true,因此非数字值传入也会返回 true ,会影响 NaN 的判断。
console.log(isNaN(1));//false
console.log(isNaN('1'));//false
console.log(isNaN('a'));//true
console.log(isNaN('a'/2));//true
2、函数 Number.isNaN 会首先判断传入参数是否为数字
,如果是数字
再继续判断是否为 NaN ,不会进行数据类型的转换,这种方法对于 NaN 的判断更为准确。
console.log(Number.isNaN(1));//false
console.log(Number.isNaN('1'));//false
console.log(Number.isNaN('a'));//false
console.log(Number.isNaN('a' / 2));//true
八、== 操作符的强制类型转换规则?
对于 == 来说,如果对比双方的类型不一样,就会进行类型转换。假如对比 x 和 y 是否相同,就会进行如下判断流程:
1、首先会判断两者类型
是否相同,相同的话就比较两者的大小;
2、类型不相同的话,就会进行类型转换;
3、会先判断是否在对比 null 和 undefined,是的话就会返回 true;
4、判断两者类型是否为 string 和 number,是的话就会将** string 转换为 number**;
5、判断其中一方是否为 boolean,是的话就会把 boolean 转为 number 再进行判断;
6、判断其中一方是否为 object 且另一方为 string、number 或者 symbol,是的话就会把 object 转为原始类型再进行判断。
九、其他值到字符串的转换规则?
1、Null 和 Undefined 类型 ,null 转换为 “null”,undefined 转换为 “undefined”,
2、Boolean 类型,true 转换为 “true”,false 转换为 “false”。
3、Number 类型的值直接转换,不过那些极小和极大的数字会使用指数形式。
4、Symbol 类型的值直接转换,但是只允许显式强制类型转换,使用隐式强制类型转换会产生错误。
5、对普通对象来说,除非自行定义 toString() 方法,否则会调用 toString()(Object.prototype.toString())来返回内部属性 [[Class]] 的值,如"[object Object]"。如果对象有自己的 toString() 方法,字符串化时就会调用该方法并使用其返回值。
obj={
}
console.log(Object.prototype.toString.call(obj));//[object Object]
十、其他值到数字值的转换规则?
1、Undefined 类型的值转换为 NaN。
2、Null 类型的值转换为 0。
3、Boolean 类型的值,true 转换为 1,false 转换为 0。
4、String 类型的值转换如同使用 Number() 函数进行转换,如果包含非数字值则转换为 NaN,空字符串为 0。
5、Symbol 类型的值不能转换为数字,会报错。
6、对象(包括数组)会首先被转换为相应的基本类型值,如果返回的是非数字的基本类型值,则再遵循以上规则将其强制转换为数字。
十一、其他值到布尔类型的值的转换规则?
以下这些是假值: • undefined • null • false • +0、-0 和 NaN • “”。
假值的布尔强制类型转换结果为 false。从逻辑上说,假值列表以外的都应该是真值。
值 | 转数字 | 转字符串 | 转布尔值 |
---|---|---|---|
undefined | NaN | “undefined” | false |
null | 0 | “null” | false |
true | 1 | “true” | |
false | 0 | “false” | |
0 | “0” | false | |
-0 | “0” | false | |
NaN | “NaN” | false | |
Infinity | “Infinity” | true | |
-Infinity | ”-Infinity” | true | |
1(非零) | “1” | true | |
{}(任意对象) | 见上文 | 见上文 | true |
[](任意数组) |
0 | ”” | true |
[9](包含一个数字元素) |
9 | “9” | true |
[”a”](其他数组) |
NaN | 使用.join()方法 | true |
function(){}(任意函数) | NaN | 见上文 | true |
十二、|| 和 && 操作符的返回值?
首先都会转化为boolean值。
||:如果判断结果为true,那就返回第一个为真的值;如果判断结果为false,那就返回第二个为假的结果。
&&:如果判断结果为true,那就返回第二个为真的值;如果判断结果为false,那就返回第一个为假的值。
console.log( 5 && 4 );//当结果为真时,返回第二个为真的值4
console.log( 0 && 4 );//当结果为假时,返回第一个为假的值0
console.log( 5 || 4 );//当结果为真时,返回第一个为真的值5
console.log( 0 || 0 );//当结果为假时,返回第二个为假的值0
console.log((3||2)&&(5||0));//5
console.log(!5);//false
十三、Object.is() 与比较操作符 “=== ” 、“ ==” 的区别?
1、 === :比较类型和值,类型不相同直接false。
2、 ==:首先判断类型,类型不一样先转化成一样的类型,然后比较值
3、Object.is() :一般情况与 === 基本相同,特殊情况比如 -0 和 +0 不再相等,两个 NaN 是相等的
Object.is(NaN,NaN);//true
十四、什么是 JavaScript 中的包装类型?
基本类型->包装类型:Object
var a = 'abc'
Object(a) // String {"abc"}
包装类型->基本类型:valueOf
var a = 'abc'
var b = Object(a)
var c = b.valueOf() // 'abc'
在 JavaScript 中,基本类型是没有属性和方法的,但是为了便于操作基本类型的值,在调用基本类型的属性或方法时 JavaScript 会在后台隐式地将基本类型的值转换为对象。
var str = '123';
console.log(str.length);//3
'123' -> String('123') -> String('123').length