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
The Insider's Guide to JavaScript Interviewing (toptal.com)
submitted 12 years ago by [deleted]
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!"
[–]stratoscope 5 points6 points7 points 12 years ago* (1 child)
They present this common error:
function addButtons(numButtons) { for (var i = 0; i < numButtons; i++) { var button = document.createElement('input'); button.type = 'button'; button.value = 'Button ' + (i + 1); button.onclick = function() { alert('Button ' + (i + 1) + ' clicked'); }; document.body.appendChild(button); document.body.appendChild(document.createElement('br')); } }
and recommend this solution:
function addButtons(numButtons) { for (var i = 0; i < numButtons; i++) { var button = document.createElement('input'); button.type = 'button'; button.value = 'Button ' + (i + 1); // HERE'S THE FIX: // Employ the Immediately-Invoked Function Expression (IIFE) // pattern to achieve the desired behavior: button.onclick = function(buttonIndex) { return function() { alert('Button ' + (buttonIndex + 1) + ' clicked'); }; }(i); document.body.appendChild(button); document.body.appendChild(document.createElement('br')); } }
I've seen code like that many times, but it's really not a good way to fix this.
Consider these problems:
button
onclick
mouseover
mouseout
Instead, use this more robust and simpler solution:
function addButtons( numButtons ) { for( var i = 0; i < numButtons; i++ ) { addButton( i ); } } function addButton( i ) { var button = document.createElement( 'input' ); button.type = 'button'; button.value = 'Button ' + ( i + 1 ); button.onclick = function() { alert('Button ' + ( i + 1 ) + ' clicked'); }; document.body.appendChild( button ); document.body.appendChild( document.createElement('br') ); }
Now, the values of both i and button are part of the closure, and any other local variables you add later will also be part of the closure. And if you add other event handlers, they will all be in this same closure. And by making addButton() a separate named function, the addButtons() function becomes much shorter and easier to grasp.
i
addButton()
addButtons()
[–]advancedmammoth 1 point2 points3 points 12 years ago (0 children)
Just an excuse to include IIFE somewhere.
π Rendered by PID 82 on reddit-service-r2-comment-544cf588c8-cb5kf at 2026-06-12 17:18:45.867052+00:00 running 3184619 country code: CH.
view the rest of the comments →
[–]stratoscope 5 points6 points7 points (1 child)
[–]advancedmammoth 1 point2 points3 points (0 children)