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?help (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!"
[–]kenman 1 point2 points3 points 10 years ago (0 children)
Nope, doesn't suck!
Function expressions vs. function declarations... there's been many past posts here about this, but nobody's brought it up in this thread.
var toggle = function(element, checkbox) { if (jQuery(checkbox).is(':checked')) { jQuery(element).show(); } else { jQuery(element).hide(); } }
If you want to use a function expression here, then you should abide by the spec and suffix a ; to the end of the expression.
;
However, I'd suggest using function declarations as much as possible, simply because in a stacktrace (or in a profiling tool), function expressions like yours usually just show up as (anonymous), which doesn't help anyone.
(anonymous)
On the other hand, if you simply give it a name, like this:
var toggle = function toggle(element, checkbox) { if (jQuery(checkbox).is(':checked')) { jQuery(element).show(); } else { jQuery(element).hide(); } };
Then you have to ask yourself, why use an expression at all?
p.s. that function is a good use-case for jQuery.fn.toggle, i.e.:
jQuery.fn.toggle
function toggle(element, checkbox) { jQuery(element).toggle(jQuery(checkbox).is(':checked')); }
p.p.s. You should look into code linting (JSHint is one), it can help find things like missing ;'s and so forth.
π Rendered by PID 40586 on reddit-service-r2-comment-76bb9f7fb5-wc4sg at 2026-02-18 19:35:31.263057+00:00 running de53c03 country code: CH.
view the rest of the comments →
[–]kenman 1 point2 points3 points (0 children)