An interview question about js type conversion?

if ([] = = false) {console.log ("a")}
if ({} = = false) {console.log ("b")}
if ([] = = {}) {console.log ("c")}
if ([]) {console.log ("d")}

the way of implicit conversion of these questions, please help me to solve

Mar.09,2022

Equality comparison between objects and non-objects

ES5 specification 11.9.3.8-9 makes the following provisions:

if Type (x) is a string or number, Type (y) is an object, return the result of x = = ToPrimitive (y);
if Type (x) is an object, Type (y) is a string or number, return the result of ToPromitive (x) = = y.

ToPrimitive is to try to return its original value, that is, string or number .

therefore:

  1. [] = false, [] will first pass valueOf to get [] , which is not the original value, and then through toString to get "" .
  2. ({}) = = false, is the same as above, and the final result is "[object Object]"
  3. Ibid. ''! = "[object Object]"
  4. does not have = , [] defaults to true

reference: "JS (middle) you don't know" , it is strongly recommended to take a look, and give a very lame [=! [] , return true !


https://codeshelper.com/a/11.
the analysis of a master planing ancestral grave, you can take a hard look at


[], {} will be implicitly converted into true

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3cc46-2c36a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3cc46-2c36a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?