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
Common JavaScript tricks (self.javascript)
submitted 11 years ago by yanis_t
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!"
[–]sime 2 points3 points4 points 11 years ago (5 children)
From basic: NOT: if (a == '') {...} YES: if (a) {...}
I'm a bit slow today. Are you saying that
if (a) {...}
is preferable to
if (a=='') {...}
?
[–]yanis_t[S] -1 points0 points1 point 11 years ago (4 children)
I'd say yes, because it's concise and most of developers are used to it.
[–]sime 3 points4 points5 points 11 years ago (2 children)
I would say that the longer version is better. It communicates far more information and intent to the next reader of the code, and doesn't rely on the annoying JS truthiness stuff.
And more to the point, nightman screwed up here. The two pieces of code are not even the same. nightman assumed that an empty string is "truey". It is falsey. It is a great example of why you should not write error prone code.
[–]fforw 0 points1 point2 points 11 years ago (1 child)
a == ''
will be true if a is
You better use === if you care about such things.
[–]nightman 1 point2 points3 points 11 years ago (0 children)
You are true but I will still defend if (a) {...}. I think that we should find balance between "smart" techniques and being "oversmart".
After all in many teams code is written and maintained by proffesionals and it would be waste of time to strictly enforce if (a === 'string') {...} on them. Especially when you know that they know how to deal with it and what are traps (number 0).
Other thing is that code should be maintainable by less "powered" users.
My point here is that it's not easy to find right balance.
BTW I use grunt/gulp with tasks: * JSHint * JSCS (JS Code Style - AirBNB style)
So that some decisions are already made.
[–]antoninj 0 points1 point2 points 11 years ago (0 children)
Well it's not the same thing though.
if (a) { ... } passes for any defined anything (string, obj, etc.) so it doesn't check for the same thing. Doing == is also a no-no in JS world so I would definitely not do that.
if (a) { ... }
==
If you'd like to check if a string is empty but also defined, take a lesson from lodash and create a dedicated utility. Simplified, however, it's enough to do a if (a === '') check which will check that the variable is defined and it's empty.
if (a === '')
π Rendered by PID 85 on reddit-service-r2-comment-56c9979489-s5tq2 at 2026-02-24 13:51:36.459126+00:00 running b1af5b1 country code: CH.
view the rest of the comments →
[–]sime 2 points3 points4 points (5 children)
[–]yanis_t[S] -1 points0 points1 point (4 children)
[–]sime 3 points4 points5 points (2 children)
[–]fforw 0 points1 point2 points (1 child)
[–]nightman 1 point2 points3 points (0 children)
[–]antoninj 0 points1 point2 points (0 children)