Home > OS >  return OR with the same value
return OR with the same value

Time:04-10

Why would someone do this

function() { return(abc || abc || "") }

isn't it's enough to do just return(abc || "")?

CodePudding user response:

You're right, in any remotely serious code, doing just return abc || "" would be enough.

The only situation in which return(abc || abc || "") could possibly result in different logic would be if you were inside a with, or if abc was a getter on the window - and these would only occur if someone was deliberately writing confusing code.

let count = 0;
Object.defineProperty(window, 'abc', { get: () => count   });

const result = abc || "";
console.log(result);

let count = 0;
Object.defineProperty(window, 'abc', { get: () => count   });

const result = abc || abc || "";
console.log(result);

  • Related