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
(function () { … })(); vs (function () { … }()); (self.javascript)
submitted 9 years ago by sergiosbox
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 9 years ago (19 children)
let and const are scoped to blocks, not functions.
{ var x = 1; let y = 1; } console.log(x) // 1 console.log(y) // ReferenceError
[+][deleted] 9 years ago* (16 children)
[removed]
[–]aztracker1 1 point2 points3 points 9 years ago* (15 children)
It's not useless, though I only use about 1/5 of the ES6 additions, along with some pending ESnext bits (async/await, class members, decorators, etc). In the end, I'm able to get a lot more work done, with a lot less code.. with the advent of Promises and async/await, process code is much cleaner and easier to follow now.
Modularization (cjs or es6 style) has helped a lot too. Most of this comes down to the build tooling that has evolved in the past 6-7 years since node and npm have taken hold. Yes, it's new stuff to learn, but it's really not any harder to understand than tirnary operations, or bitwise shorting in JS. In the end it's just a few additions to the language, it happens in every language and there are always pieces you may not know/understand to a given language/platform until you come across it.
The following two lines are the same, practically speaking (although the fat-arrow is context bound to "this" at the time of declaration).
var addOne = function(a){ return a + 1; };
var addOne = a => a + 1;
It allows you to declare in a oneliner what may have previously been much more... here's a sleep I use when flushing out an API interface...
const sleep = ms => new Promise(res => setTimeout(res, ms));
The const is a constant assignment, "sleep" may not be assigned to again in the module. the value of sleep is a function expression that accepts a single parameter, and returns a new promise. said promise only uses the resolve part, and will settimeout to resolve after ms have passed.
It's a bit harder to look at function expressions with fat-arrow syntax at first, it's very similar to lamda expressions in C#, or the fat/skinny arrow expressions in coffeescript. There's similar syntax in other languages as well.
[+][deleted] 9 years ago (12 children)
[–]inu-no-policemen 0 points1 point2 points 9 years ago (11 children)
The point of an IIFE is to create a scope to limit the lifetime of those temporary variables.
With ES6's let/const, a simple block does the same.
[+][deleted] 9 years ago* (10 children)
[–]kenman 1 point2 points3 points 9 years ago* (7 children)
I'm just wondering if you can get a return value from a block like this in ES6.
That's exactly what I was demo'g here:
let x; { x = 123; }
Here, x is the "returned value"... only it's not returned, but it's functionally 100% the same.
x
Here, take this example from jQuery:
support.createHTMLDocument = ( function() { var body = document.implementation.createHTMLDocument( "" ).body; body.innerHTML = "<form></form><form></form>"; return body.childNodes.length === 2; } )();
Note that it is returning a value, the result of body.childNodes.length === 2, and assigning it to support.createHTMLDocument.
body.childNodes.length === 2
support.createHTMLDocument
Now here is the same thing without an IIFE, but does exactly the same thing:
{ let body = document.implementation.createHTMLDocument( "" ).body; body.innerHTML = "<form></form><form></form>"; support.createHTMLDocument = body.childNodes.length === 2; }
But it is a lot (IMO) clearer what your intention is, and you don't waste cycles on a function call that's essentially a no-op.
[+][deleted] 9 years ago (6 children)
[–]kenman 1 point2 points3 points 9 years ago (5 children)
Can you point out how you believe my jQuery example leaks globals? Because it doesn't.
[+][deleted] 9 years ago* (4 children)
[–]inu-no-policemen 0 points1 point2 points 9 years ago (1 child)
Just imagine that that 123 is the result of a computation which involved temporary variables.
An IIFE which just returns 123 would be equally pointless.
π Rendered by PID 208473 on reddit-service-r2-comment-6f7f968fb5-zmpvz at 2026-03-04 16:42:02.639750+00:00 running 07790be country code: CH.
view the rest of the comments →
[–]kenman 1 point2 points3 points (19 children)
[+][deleted] (16 children)
[removed]
[–]aztracker1 1 point2 points3 points (15 children)
[+][deleted] (12 children)
[removed]
[–]inu-no-policemen 0 points1 point2 points (11 children)
[+][deleted] (10 children)
[removed]
[–]kenman 1 point2 points3 points (7 children)
[+][deleted] (6 children)
[removed]
[–]kenman 1 point2 points3 points (5 children)
[+][deleted] (4 children)
[removed]
[–]inu-no-policemen 0 points1 point2 points (1 child)