JSFiddle - React, Tailwind, and code Playground

by Luis Perez

HTML

results in console output

JavaScript

function toPrimitive(obj) {
    var value = obj.valueOf();
    if(obj !== value) return value;
    return obj.toString();
}

// loseEqual() behaves just like `==`
function loseEqual(x, y) {
    // notice the function only uses "strict" operators 
    // like `===` and `!==` to do comparisons
    
    if(typeof y === typeof x) return y === x;

    // treat null and undefined the same
    var xIsNothing = (y === undefined) || (y === null);
    var yIsNothing = (x === undefined) || (x === null);
    
    if(xIsNothing || yIsNothing) return (xIsNothing && yIsNothing);

    if(typeof y === "function" || typeof x === "function") {
        // if either value is a string convert the
        // function into a string and compare
        if(typeof x === "string") {
            return x === y.toString();
        } else if(typeof y === "string") {
            return x.toString() === y;
        } 
        
        return false;
    }

    if(typeof x === "object") x = toPrimitive(x);
    if(typeof y === "object") y = toPrimitive(y);
    
    if(typeof y === typeof x) return y === x;

    // convert x and y into numbers if they are not already use the "+" trick
    if(typeof x !== "number") x = +x;
    if(typeof y !== "number") y = +y;
    
    return x === y;
}

function loseEqual2(x, y) { // functions treated like objects - DOESN'T WORK
    // notice the function only uses "strict" operators 
    // like `===` and `!==` to do comparisons
    
    if(typeof y === typeof x) return y === x;

    // treat null and undefined the same
    var xIsNothing = (y === undefined) || (y === null);
    var yIsNothing = (x === undefined) || (x === null);
    
    if(xIsNothing || yIsNothing) return (xIsNothing && yIsNothing);

    if(typeof x === "object" || typeof x === "function") x = toPrimitive(x);
    if(typeof y === "object" || typeof y === "function") y = toPrimitive(y);
    
    if(typeof y === typeof x) return y === x;

    // convert x and y into numbers if they are not already...