singularity

Coercion

ToPrimitive

valueOf

ToNumber

ToString

ToBoolean

Summary of abstract equality operator ==

rules of ==

  1. If the values being compared are of the same type, then perform the strict equality comparison⁶⁶ ===

  2. If one value is undefined or null and the other value is also undefined or null, return true console.log(null === undefined); // false so only check for either null or undf

  3. If one or both values are objects, they are converted into primitive types, preferring the number type

  4. If both values are primitives but are of different types, convert the types until they match, preferring the number type for coercing values


const someVal = {};

if (someVal == true) {
  console.log("true");
} else {
  console.log("false");
}

// else 

// 1. rule 4: someVal == 1
// 2. rule 4: "[obj Obj]" == 1
// 3. rule 4: "NaN" == 1
// 4. rule 4: NaN == 1



const someVal = {};

if (someVal) {
  console.log("true");
} else {
  console.log("false");
}

// true

Relational operators


    console.log([13] == 13);
    // single element coerced -> "13", true


    console.log([1] < [2]);
    // true


    console.log([undefined] == 0);
    //"" == 0 -> 0 = 0 -> true

Operator Precedence


    console.log([] == ![]);
    // Not operator takes precedence over ==
    // 1) [] -> true -> ! -> false
    // 2) [] == false -> [] == 0 -> "" == 0
    // 3) 0 == 0 = true



    console.log(!!"true" == !!"false");
    //!!+string == !!+string
    //!!true == !!true -> true