Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

return OR with the same value

Why would someone do this

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading