Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] 第1364天 请解释下true == []的结果 #5321

Open
haizhilin2013 opened this issue Jan 8, 2023 · 2 comments
Open

[js] 第1364天 请解释下true == []的结果 #5321

haizhilin2013 opened this issue Jan 8, 2023 · 2 comments
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第1364天 请解释下true == []的结果

3+1官网

我也要出题

@haizhilin2013 haizhilin2013 added the js JavaScript label Jan 8, 2023
@ShihHsing
Copy link

ShihHsing commented Jan 9, 2023

根据 JavaScript 的类型转换规则,当一个布尔值与一个非布尔值进行比较时,非布尔值会被强制类型转换成布尔值再进行比较。在这种情况下,如果非布尔值被转换为 true,那么 true == 非布尔值 的结果为非布尔值。

对于 true == [],其中 [] 被转换为布尔值为 true,因为空数组在进行布尔值转换时会被视为真值。因此,该比较表达式会被解释为 true == true,结果为 true

扩展阅读

https://www.yuque.com/shih_hsing/nub4ks/occk2vlgnwd9zwi9?singleDoc# 《==与===区别》

@moli0722
Copy link

根据运算符的比较规则,true, []都将最终被转化为Number类型进行比较,true -> 1,[] -> 0。所以最后true == [] 为false。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests

3 participants