JSFiddle - React, Tailwind, and code Playground

JavaScript

obj = { x:555, y: "hi" };
obj.myself = obj;

alert(print(obj));

obj.myself={a:'hello'};
alert(print(obj));

function print(obj){
    try{
        seen = [];
        json = JSON.stringify(obj, function(key, val) {
           if (typeof val == "object") {
                if (seen.indexOf(val) >= 0) return;
                seen.push(val)
            }
            return val;
        });
        return json;
    }catch(e){
        return e;
        
    }
}