question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The use of || operator in assignments

See original GitHub issue

In 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:open
  • Created 7 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
getifycommented, Aug 7, 2016

Just FYI: technically the a || b and a ? a : b are not strictly equivalent (but very close). When the a 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 evaluates a only once, and a ? a : b evaluates a twice.

1reaction
felipethomecommented, Aug 3, 2016

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found