'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 || "")?



Solution 1:[1]

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);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CertainPerformance