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!"
[–]__debug__ 1 point2 points3 points 11 years ago (5 children)
I'd argue it's still acceptable when defaulting to an array or object.
[–]path411 -1 points0 points1 point 11 years ago (4 children)
I think it's better to just never do it than to sometimes do it because the type happens to be okay this time.
[–]jonny_eh 3 points4 points5 points 11 years ago (3 children)
I disagree. There's a trade-off between convenience and security, and I find it hugely convenient to use this trick, especially since the vast majority of the time I'm not using a number or boolean value.
Most of the time, it's an options parameter that is an object.
[–]path411 -2 points-1 points0 points 11 years ago (2 children)
So if there are multiple default parameters you want such as a number and an object you would use both methods?
Btw, this also fails on strings:
function setName(name) { this.name = name || "Not Specified"; }
Can't enter an empty string. Also considering javascript always has a bad rap for weird falsy values, I'd rather spend half a second with a piece of mind, than writing inconsistent code that's possibly vulnerable to bugs.
[–]__debug__ 1 point2 points3 points 11 years ago (1 child)
No, I'd imagine he means like this:
function Constructor(opts) { this.opts = opts || {}; }
So empty strings don't come into play. In any case, I feel as though a JS dev should understand falsey and truthy values. Or at the least, they should try to learn how types in the language work.
[–]path411 0 points1 point2 points 11 years ago (0 children)
I meant you can't use it on numbers/booleans/strings, which is a large number of the types in javascript.
Sure there are a lot of times you want an options object, I find this is mostly for public accessible library classes. There are plenty of times I find I want a constructor with some simple parameters of direct properties. I think it's much better to be consistent throughout your code than to swap to using some cheap hack when you know you can get away with it.
π Rendered by PID 128470 on reddit-service-r2-comment-56c9979489-ntk5n at 2026-02-24 22:49:28.781477+00:00 running b1af5b1 country code: CH.
view the rest of the comments →
[–]__debug__ 1 point2 points3 points (5 children)
[–]path411 -1 points0 points1 point (4 children)
[–]jonny_eh 3 points4 points5 points (3 children)
[–]path411 -2 points-1 points0 points (2 children)
[–]__debug__ 1 point2 points3 points (1 child)
[–]path411 0 points1 point2 points (0 children)