javascript语法的问题:严格等于和等于区别

本文深入探讨了JavaScript中未定义类型和空类型的概念,并详细解析了等于(==)和全等(===)操作符的区别及使用场景,通过实例展示了不同类型间的比较规则。

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

The Undefined type 未定义类型the Undefined type has only one value, undefined. When a variable is declared and not initialized, it is given the value of undefined by default.
未定义类型,只有一个值undefined只要你定义了变量没有赋值,那么它的值就是undefined

The Null type 空类型Another type with just one value, the Null type, has only the special value null, which is also its literal. The value undefined is actually a derivative of the value null, so ECMAScript defines them as equal to each other.
空类型也只有一个值null,未定义类型的值undefined 其实是空类型值null的一个衍生值,所以ECMAScript定义他俩相等

Equal and not equal 等于和不等于The equal operator in ECMAScript is the double equal sign (==), and it returns true if—and only if—both operands are equal. The not equal operator is the exclamation point followed by an equal sign (!=), and it returns true if—and only if—two operands are not equal. Both operators do conversions in order to determine if two operands are equal. 等于和不等于用法和其他类型语言大致相同,但是又有很多不同,比如,你在比较相同的字符数字和数字类型数字式会“自动转换”后比较。

毕竟JS是弱类型语言,一些特例在下面
1.null == undefined true
2.“NaN” == NaN false
3.5 == NaN false
4.NaN == NaN false
5.NaN != NaN true
6.false == 0 true
7.true == 1 true
8.true == 2 false
9.undefined == 0 false
10.null == 0 false
11.“5” == 5 true
Identically equal and not identically equal全等(值和类型)和不全等The brothers of the equal and not equal operators are the identically equal and not identically equal operators. These two operators do the same thing as equal and not equal, except that they do not convert operands before testing for equality. The identically equal operator is represented by three equal signs (===) and only returns true if the operands are equal without conversion. 一句话解释:用===和!==,比较前做比较的值不会做转换。

至于内存地址比较说,我不敢苟同,JavaScript是弱类型,虽然分着几个类型的,如String,Number,Boolean,Object,我个人认为,==和===都应该是值比较,至于用==产生的特例情况应当是ECMAScript设计之初就加进去的特性,===是instanceof && ==
也就是说===更符合强类型语言规范

附送一个(==)转换规则 When performing conversions, follow these basic rules:

❑ If an operand is a Boolean value, convert it into a numeric value before checking for equality.A value of false converts to 0; whereas a value of true converts to 1.

❑ If one operand is a string and the other is a number, attempt to convert the string into a number before checking for equality.

❑ If one operand is an object and the other is a string, attempt to convert the object to a string (using the toString() method) before checking for equality.

❑ If one operand is an object and the other is a number, attempt to convert the object to a number before checking for equality.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值