js基本类型

本文深入探讨JavaScript的六种基本数据类型:number、string、null、undefined、boolean及ES6新增的symbol。解析不同类型的typeof输出结果,讨论对象与布尔值转化规则,以及特殊值在if判断中的行为。同时,对比new操作符前后变量的行为差异,以及全局函数的使用误区。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值