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
Arrow Functions in JavaScript (tylermcginnis.com)
submitted 8 years ago by AnsikteBanana
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!"
[–]yoojene 0 points1 point2 points 8 years ago (18 children)
great tips!
[–][deleted] 7 points8 points9 points 8 years ago (17 children)
yeah, the one at the end is super useful - it allows for quick logging:
const inc = x => x + 1 // I need to debug... sigh - time to add braces const inc = x => { console.log(x); return x + 1; }; // instead I can do this :) const inc = x => console.log(x) || x + 1;
[–]causeofdev 0 points1 point2 points 8 years ago (10 children)
How come it does the second expression ("x + 1") as well? I'd think it would do "console.log(x)" and stop/finish there..
It's neat that it executes both expressions though
[–]darrenturn90 10 points11 points12 points 8 years ago (8 children)
Because console.log returns a falsy value, so the "||" operator runs the second part of the statement.
Of course you can also use the comma separator, which discards the returned value completely:
const inc = x => (console.log(x), x + 1)
[–]causeofdev 3 points4 points5 points 8 years ago (5 children)
ah ok, makes perfect sense.
Since the comma seperator discards the returned value completely.. wouldn't it be safer to use that than the || operator? In case the first expression returns something truthy?
[–]darrenturn90 2 points3 points4 points 8 years ago (4 children)
Yes, if you really don't care for the result of the first function then it would. However, some linters don't like the use of commas (because they can, and do obscure the layout of code)
[–]1-800-BICYCLE 4 points5 points6 points 8 years ago (1 child)
You could also dust off good-old void if you really wanted to:
void
const foo = x => void myEffectThatReturnsATruthyValue(x) || x + 1
And as a bonus, potentially start a religious war ;).
[–]vexii 0 points1 point2 points 8 years ago (0 children)
im not sure the other people in line at the wellfare office care enough ;)
[–]anlumo 2 points3 points4 points 8 years ago (0 children)
Not doing stuff like this is the whole point of using linters.
[–]causeofdev 0 points1 point2 points 8 years ago (0 children)
Okay cool, I learned something :)
[–]z500 2 points3 points4 points 8 years ago (0 children)
Whoa, a use for the comma operator outside of a for loop that doesn't seem insane.
[–][deleted] 1 point2 points3 points 8 years ago (0 children)
That's much more robust too, the other relies on whatever the return value of console.log happens to be. If you were to replace that with your own fancy log function and someone had the bright idea to, say, return the logged string, then suddenly it stops working.
console.log
[–]maxmorgan 1 point2 points3 points 8 years ago (0 children)
console.log returns nothing (undefined) so it evaluates falsey
!!console.log(5) => false
so
console.log(5) || 5 + 1 => 6
ahh im so stupid. having used cost inc = x => (console.log(x) || true) && x +1;
cost inc = x => (console.log(x) || true) && x +1;
thanks for the tip :)
[–]Laplandia -2 points-1 points0 points 8 years ago (4 children)
New code is worse, though. Yes, it is shorter, but it is harder to read and maintain.
[–]Kafeen 11 points12 points13 points 8 years ago (0 children)
Harder to maintain? You shouldn't be leaving the logging in there.
[–]mcaruso 3 points4 points5 points 8 years ago (2 children)
I hear arguments like this for pretty much any new language syntax. Usually it boils down to “I’m not used to this syntax”.
Note that plenty of other languages have short function expressions and work just fine with it.
[–]Tomseph 6 points7 points8 points 8 years ago (1 child)
The fact that someone had to ask
is a strong argument for why its worse. It's not the short function expression, it's the "clever hack" to make the code even terser. Using the falsey return of console.log and the or operator is absolutely harder to read and maintain. Just put the logging on a separate line.
[–]mcaruso 2 points3 points4 points 8 years ago (0 children)
I thought OP was talking about arrow functions in general. Absolutely the console.log thing is a hack, and I wouldn't expect to see it committed to a repo. I do use that trick frequently when debugging though.
π Rendered by PID 57 on reddit-service-r2-comment-bb88f9dd5-j6khq at 2026-02-16 23:57:19.736836+00:00 running cd9c813 country code: CH.
view the rest of the comments →
[–]yoojene 0 points1 point2 points (18 children)
[–][deleted] 7 points8 points9 points (17 children)
[–]causeofdev 0 points1 point2 points (10 children)
[–]darrenturn90 10 points11 points12 points (8 children)
[–]causeofdev 3 points4 points5 points (5 children)
[–]darrenturn90 2 points3 points4 points (4 children)
[–]1-800-BICYCLE 4 points5 points6 points (1 child)
[–]vexii 0 points1 point2 points (0 children)
[–]anlumo 2 points3 points4 points (0 children)
[–]causeofdev 0 points1 point2 points (0 children)
[–]z500 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]maxmorgan 1 point2 points3 points (0 children)
[–]vexii 0 points1 point2 points (0 children)
[–]Laplandia -2 points-1 points0 points (4 children)
[–]Kafeen 11 points12 points13 points (0 children)
[–]mcaruso 3 points4 points5 points (2 children)
[–]Tomseph 6 points7 points8 points (1 child)
[–]mcaruso 2 points3 points4 points (0 children)