Path: csiph.com!3.eu.feeder.erje.net!feeder.erje.net!news.mb-net.net!open-news-network.org!.POSTED.178.197.205.75!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Understanding && Date: Sat, 17 Apr 2021 17:05:11 +0200 Organization: PointedEars Software (PES) Lines: 87 Message-ID: <21031565.EfDdHjke4D@PointedEars.de> References: <87wnt2e06e.fsf@bsb.me.uk> <87mttydm95.fsf@bsb.me.uk> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit Injection-Info: gwaiyur.mb-net.net; posting-host="178.197.205.75"; logging-data="24786"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.10 Cancel-Lock: sha1:dBH8aX0IFG0CmyWD6FCjCg6V/X0= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Jon Ribbens writes: >> On 2021-04-16, Ben Bacarisse wrote: >>> Jon Ribbens writes: >>>> You most often see this sort of thing in JavaScript that was >>>> automatically generated by a compiler (either because the original >>>> source was in something else like TypeScript or a more modern version >>>> of JavaScript or because the author wanted to compress/minify the >>>> script), but human beings are also quite capable of writing >>>> hard-to-read code like this. >>> >>> Is that specifically when the right-hand operand is an assignment >>> expression? I don't find that hard to read but then I'm used to >>> assignment being and expression so it seems like a natural extension of >>> the more frequently seen uses of &&. >> >> I mean where it's an expression being abused as a control flow >> mechanism. > > If readability is not the problem I'm not sure what constitutes abuse. To begin with: One is a *statement*, the other an *expression*. Those are two different kinds of token in a programming language (certainly in ECMAScript implementations). Also, such code easily makes newcomers think that a && b || c would be equivalent to if (a) b; else c; but it is NOT: In the former case, if “a” evaluates to a false-value, then “c” is evaluated; but if “a” evaluates to a true-value, then “b” is evaluated, and “c” is *also* evaluated afterwards if “b” evaluates to a false-value. Whereas with in the latter case, “c” is never evaluated if “a” is evaluates to a true-value. A special case is that “b” is an assignment expression in the literal sense. Perhaps counter-intuitively, the result of such an expression is NOT the evaluated left-hand side *after* assignment but the evaluated *right-hand* side *before* assignment. This is important to realize, for example, if the left-hand side refers to a property as that may be read-only or have a setter: Suppose that “o” refers to an object that has a property named “p”, then the "shortcut" let success = (o.p = 42) === 42; will always assign “true” to “success”, even if o.p does not actually have the value "42" afterwards (i.e. the assignment was NOT successful), and the "shortcut" (o.p = 42) === 42 && doSomething(); will *always* call doSomething(). Patterns like var x = x || defaultValue; instead of if (!x) var x = defaultValue; are equally problematic. [Notably, let x = x || defaultValue; is not equivalent to if (!x) let x = defaultValue; Both are actually *invalid* code (even if “defaultValue” is declared), but the first one produces a ReferenceError (“x is not defined“) while the second one produces a SyntaxError (“Lexical declaration cannot appear in a single-statement context”).] -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.