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

Menu