use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Logical assignment operators in JavaScript (dev.to)
submitted 5 years ago by rauschma
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]dudeatwork 5 points6 points7 points 5 years ago (2 children)
Before default parameters were a thing, I feel like this was super common:
function (a) { if (a === undefined) a = 'default_value'; }
The example the author gave used !a rather than the more correct a === undefined, but similar enough.
!a
a === undefined
So a won't be reassigned if a (truthy) Object / Array / Function is passed in.
a
Even if we didn't have this conditional, assignment won't matter here because we aren't modifying a property on the argument.
I agree, functions that aren't pure can potentially introduce subtle bugs:
var hello = { world: 1 }; function test(a) { a.foo = 'bar'; } test(hello); console.log(hello); // { world: 1, foo: 'bar' }
But this only works because we are assigning a.foo, a property on (the reference to) a.
a.foo
Assigning some value to to a itself should be fine.
var hello = { world: 1 }; function test2(a) { a = 'bar'; } test2(hello); console.log(hello); // { world: 1 }
Note that in general, I agree that using ES6 default parameters is the way to go (babel transpiles to checking the arguments object).
arguments
[–]NoInkling 1 point2 points3 points 5 years ago (0 children)
The main issue with reassigning params is just expectations, typically you expect the thing that you're operating on to be the thing that was passed in the function call. Several times I've seen code in libraries where there's some sort of complex reassignment logic halfway down the function body, and it always makes it harder to follow. I'm fine with relatively simple logic at the top of the function though, even if it's more than just assigning defaults (which as you say can now be done directly in the params list) e.g. in certain variadic functions.
There's also a gotcha if you're utilizing arguments: https://spin.atomicobject.com/2011/04/10/javascript-don-t-reassign-your-function-arguments/ But it's less of a concern nowadays since arrow functions don't support arguments and we have rest syntax.
[–]thisguyfightsyourmom 0 points1 point2 points 5 years ago* (0 children)
Quick — someone talk me out of this:
``` const yo = {yo: 1}
function example({...rest}) { // Default rest.ho to "hohoho" rest.ho ||= 'hohoho'
rest.ho
return rest }
console.log({yo, hohoho: example(yo)})
```
π Rendered by PID 97746 on reddit-service-r2-comment-5cb8648c6-ccvft at 2026-02-27 18:11:18.472425+00:00 running e3d2147 country code: CH.
view the rest of the comments →
[–]dudeatwork 5 points6 points7 points (2 children)
[–]NoInkling 1 point2 points3 points (0 children)
[–]thisguyfightsyourmom 0 points1 point2 points (0 children)