The use of || operator in assignments
See original GitHub issueIn the topic 15.7 this kind of assignment is favored over ternaries: const foo = a || b;
. Actually, this pattern is used in a lot of the examples, but should it be discouraged since it can produce undesired behavior when the left operand is falsy? In the above example if a
is falsy so b
would be the result.
I know the developer should distinguish problematic cases, but one of the goals of the style guide is to avoid potential issues.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Assignment Operators in C/C++ - GeeksforGeeks
Assignment operators are used to assigning value to a variable. The left side operand of the assignment operator is a variable and right ......
Read more >C++ Assignment Operators - W3Schools
Assignment operators are used to assign values to variables. In the example below, we use the assignment operator ( = ) to assign...
Read more >What is Assignment Operator? - Definition from Techopedia
An assignment operator is the operator used to assign a new value to a variable, property, event or indexer element in C# programming ......
Read more >Assignment operators - Microsoft Learn
Assignment operators store a value in the object specified by the left operand. There are two kinds of assignment operations: simple assignment, ...
Read more >Assignment operators - IBM
An assignment expression stores a value in the object designated by the left operand. There are two types of assignment operators: ... The...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just FYI: technically the
a || b
anda ? a : b
are not strictly equivalent (but very close). When thea
expression is something more than an identifier (an object prop access, a function, etc), if evaluating that expression has side effects, the outcomes will be different in the two cases.a || b
evaluatesa
only once, anda ? a : b
evaluatesa
twice.Actually, it seems 7.7 already explains that in the comments of the example. Maybe the content of the comment presented as a
Why?
quote would be more effective?