js基本类型

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31214097/article/details/85228571

ES5共有5种基本类型,分别为number,string,null,undefined,boolean,ES6新增了symbol.

console.log(typeof undefined); 输出为undefined

console.log(typeof null); 输出为object

console.log(typeof [1]); 输出为object

console.log(typeof 1); 输出为number

console.log(typeof "1"); 输出为string

console.log(typeof true); 输出为boolean

任何对象转化为布尔值时,if判断都为true,在js中,只有NaN,0,-0,“”,null,undefined这6个值转化为布尔值时,才为false.

加new表示创建对象,不加new表示类型转换。

var a = new Boolean(0);

if(a){  //表示判断对象

console.log(222);

}

可以正常输出

if(undefined){ //判断boolean时,undefined为false

console.log(222);

}

全局函数中,setTimeOut(),不是全局函数。decodeURI,encodeURI等都是全局函数。

readonly属性只有在input(text/password)有效,而disabled对于所有的表单元素均有效,包括select,radio,checkbox,button等。

document.getElementById("button").setAttribute("disabled",true);

document.getElementById("button").disabled = true;
==符号判断相等的时候 如果一个值是null,一个值是undefined,则他们相等。

如果一个值是数字,另一个值是字符串,则先将字符串转化为数字,然后再进行比较。

如果其中有一个值为true,则将其转化为1再进行比较,如果其中有一个为false,则转为0再进行比较。

如果有一个值为对象,另一个是数字或者字符串,则将对象转为原始值,再进行比较。

console.log([ ]) ? true:false) 输出为true

console.log([] == false ? true:false) 等价为console.log(0 == 0 ? true : false);  输出为true

console.log({} == false) ? true : false) 等价为console.log((NaN == 0) ? true : false) 输出为false

猜你喜欢

转载自blog.csdn.net/qq_31214097/article/details/85228571