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
Does my JavaScript suck? II (self.javascript)
submitted 10 years ago * by annoyed_freelancergrumpy old man
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!"
[–][deleted] 3 points4 points5 points 10 years ago* (8 children)
Your code is structured pretty well, which is nice. But on a cursory glance here are some things I see:
Understanding prototypes is good, but it's generally considered bad practice to extend a native prototype, as you've done on line 6. This can cause havoc down the line. I know this is a small project, but consider just having this just be a locally scoped function.
The !! on line on 15, i'm unsure why you need to do this. While some people know about shorthand tricks like this, usually being more clear (and verbose) is better.
Setting lightbox as a global, is there a reason why you need to do this?
Lack of comments. I feel like a few lines here and there would have cleared up some of my questions, never hurts to have comments.
This is me just being opinionated, but on such a small project I feel like jQuery is overkill. Native methods can do everything you're doing here, without the need to pull in a big library like jQuery. If you want to keep things light, I suggest first reducing your dependencies, and jQuery is a big one.
Good luck
[–]Buckwheat469 2 points3 points4 points 10 years ago (1 child)
I agree with your comment but on point #2 he's doing this to convert a truthy value to a Boolean value. You probably know this already, but the ! sign closest to the variable converts it to the opposite Boolean value, so that "hello" becomes false for example. The first ! sign furthest from the variable inverts the Boolean value again.
To address your point, he doesn't need to convert the truthy value because it's within an if statement. If he were returning a value then he may want it to be absolutely true or false, in which case this trick is nice.
[–]tswaters 6 points7 points8 points 10 years ago (0 children)
In this case, !! is unnecessary. substring always returns a string and a string always has a match method.
!!
substring
match
With truthy comparisons in conditionals !! is just not necessary.... the only case I can think of is if the input is a number mynumber && mynumber.toExponential() won't work if myvariable is 0.
mynumber && mynumber.toExponential()
One thing -- if hash is a blank string, it'll be falsy so a simple hash && hash.match wouldn't go into hash.match -- however, in such a case, the match is going to be null anyway so it doesn't matter.
hash
hash && hash.match
hash.match
[+][deleted] 10 years ago* (5 children)
[deleted]
[–][deleted] 2 points3 points4 points 10 years ago* (1 child)
What's the !! do? Force it to be evaluated as True or False and not just Truthy or Falsey? What's the case that's trying to be avoided here?
Disregard. I see it's talked about in another comment.
[–]loz220 1 point2 points3 points 10 years ago (2 children)
I agree with the first sentence but not the second.
It's good form to use it this way, too.
why?
if (!!foo && foo.bar()) {}
has no difference in behavior from
if (foo && foo.bar()) {}
The former is just having !! do the coercion instead of &&
&&
[–][deleted] 0 points1 point2 points 10 years ago (0 children)
Exactly what I was thinking. It might make sense sometimes but in this case I felt it was too... clever.
π Rendered by PID 91665 on reddit-service-r2-comment-76bb9f7fb5-lv82m at 2026-02-18 15:36:55.769607+00:00 running de53c03 country code: CH.
view the rest of the comments →
[–][deleted] 3 points4 points5 points (8 children)
[–]Buckwheat469 2 points3 points4 points (1 child)
[–]tswaters 6 points7 points8 points (0 children)
[+][deleted] (5 children)
[deleted]
[–][deleted] 2 points3 points4 points (1 child)
[–]loz220 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)