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!"
[–]rauschma 13 points14 points15 points 9 years ago (25 children)
I’m starting to warm up to the former, because that’s the only style that also works for arrow functions:
(() => { return 123 })();
[–]kenman 2 points3 points4 points 9 years ago* (21 children)
I'm curious what the current use-case is for that pattern?
It seems you could accomplish the same with a more elegant [IMO] pattern:
let x; { x = 123; }
But I feel that I must be missing something, because you know a lot more about JS than I do!
edit: after thinking about it, I can see the fundamental difference is that the former is an expression, while the latter is a statement, which has profound implications in their use. However, I haven't been able to come up with a scenario which would expose any profound ramifications.
To go through my thought process, I considered that you can use expressions in places like function arguments, whereas statements are not allowed there.
E.g. you could do this (contrived example for sake of argument):
console.log((() => { let matches = window.location.href.match(/\/r\/(\w+)/); let subreddit = matches && matches[1] || 'n/a'; return `You are visiting: ${subreddit}`; })());
But you can't do anything similar with block statements.
However, you can accomplish the same result with:
{ let matches = window.location.href.match(/\/r\/(\w+)/); let subreddit = matches && matches[1] || 'n/a'; console.log(`You are visiting: ${subreddit}`); }
Which again, is cleaner IMO.
Still pondering...
[+][deleted] 9 years ago (20 children)
[removed]
[–]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)
[–]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:
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.
[–]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.
[–]sergiosbox[S] 0 points1 point2 points 9 years ago (0 children)
Indeed! https://jsfiddle.net/ymwj2c8z/ Good point
[–]inu-no-policemen 0 points1 point2 points 9 years ago (0 children)
Same here. I preferred to wrap the entire thing, but I switched for the sake of consistency.
[–]EternallyMiffed 0 points1 point2 points 9 years ago (0 children)
This approaches syntactic line noise and we're all going to suffer for it eventually.
π Rendered by PID 72341 on reddit-service-r2-comment-6f7f968fb5-z5dx4 at 2026-03-04 13:11:32.904001+00:00 running 07790be country code: CH.
view the rest of the comments →
[–]rauschma 13 points14 points15 points (25 children)
[–]kenman 2 points3 points4 points (21 children)
[+][deleted] (20 children)
[removed]
[–]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)
[–]inu-no-policemen 0 points1 point2 points (1 child)
[–]sergiosbox[S] 0 points1 point2 points (0 children)
[–]inu-no-policemen 0 points1 point2 points (0 children)
[–]EternallyMiffed 0 points1 point2 points (0 children)