Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #39961

Re: Understanding &&

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: Understanding &&
Date 2021-04-17 17:05 +0200
Organization PointedEars Software (PES)
Message-ID <21031565.EfDdHjke4D@PointedEars.de> (permalink)
References <U_CdnQWSXulHj-T9nZ2dnUU7-evNnZ2d@westnet.com.au> <slrns7ishn.4j8.jon+usenet@raven.unequivocal.eu> <87wnt2e06e.fsf@bsb.me.uk> <slrns7jb5v.4j8.jon+usenet@raven.unequivocal.eu> <87mttydm95.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse wrote:

> Jon Ribbens <jon+usenet@unequivocal.eu> writes:
>> On 2021-04-16, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
>>> Jon Ribbens <jon+usenet@unequivocal.eu> 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: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Understanding && Andrew Poulos <ap_prog@hotmail.com> - 2021-04-16 14:30 +1000
  Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-16 11:00 +0100
  Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-16 11:17 +0000
    Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-16 16:09 +0100
      Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-16 15:26 +0000
        Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-16 21:09 +0100
          Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-16 21:06 +0000
            Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-17 02:01 +0100
              Re: Understanding && The Natural Philosopher <tnp@invalid.invalid> - 2021-04-17 08:33 +0100
              Re: Understanding && John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-17 12:06 +0100
                Re: Understanding && The Natural Philosopher <tnp@invalid.invalid> - 2021-04-17 14:13 +0100
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-17 15:50 +0100
              Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-17 15:21 +0000
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-17 23:46 +0100
                Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-17 23:46 +0000
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-18 23:59 +0100
                Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-19 13:56 +0000
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-19 23:08 +0100
                Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-19 22:52 +0000
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-20 01:23 +0100
                Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-20 01:08 +0000
              Re: Understanding && Arno Welzel <usenet@arnowelzel.de> - 2021-04-18 00:49 +0200
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-18 17:26 +0100
          Re: Understanding && Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-17 17:05 +0200
            Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-17 23:04 +0100
              Re: Understanding && Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-18 00:48 +0200
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-18 17:13 +0100
                Re: Understanding && Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-04-19 21:58 +0200
            Re: Understanding && John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-18 18:20 +0100
          Re: Understanding && Arno Welzel <usenet@arnowelzel.de> - 2021-04-18 00:40 +0200
            Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-18 00:14 +0100
              Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-18 00:01 +0000
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-18 17:44 +0100
                Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-18 17:51 +0000
              Re: Understanding && Arno Welzel <usenet@arnowelzel.de> - 2021-04-19 13:41 +0200
                Re: Understanding && John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-19 15:02 +0100
                Re: Understanding && The Natural Philosopher <tnp@invalid.invalid> - 2021-04-20 17:40 +0100
                Re: Understanding && John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-20 18:19 +0100
                Re: Understanding && The Natural Philosopher <tnp@invalid.invalid> - 2021-04-21 10:11 +0100
                Re: Understanding && John G Harris <niam@jghnorth.org.uk.invalid> - 2021-04-21 19:46 +0100
                Re: Understanding && The Natural Philosopher <tnp@invalid.invalid> - 2021-04-22 13:48 +0100
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-19 21:28 +0100
                Re: Understanding && Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-04-19 21:56 +0000
                Re: Understanding && Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-19 23:13 +0100

csiph-web