News for hackers. Stuff that matters.
Re:Bling – the $ of jQuery without the jQuery by riquito on Monday June 15, 2015 @05:57AM (#9718410)
> 2) Semicolons are still in debate amongst the JS community, I don't think there's a consensus on whether they're needed or not and it seems to boil down to personal preferences.

They are needed.

This

    var func = function(x) { console.log(x); };
    func;
    (42);
is different from this

    var func = function(x) { console.log(x); }
    func
    (42)
Re:Bling – the $ of jQuery without the jQuery by wldlyinaccurate on Monday June 15, 2015 @06:21AM (#9718475)
You're right that your (very contrived) examples are different. But if anybody on my team writes code like this, it doesn't pass code review.
Re:Bling – the $ of jQuery without the jQuery by bshimmin on Monday June 15, 2015 @07:26AM (#9718657)
Sure, those examples are quite contrived. Here's one that's not:

  console.log("woof")
  (function() { console.log("foo") })()
Without semi-colons, that blows up (with a super unhelpful error message, to boot). Sure, if you understand ASI in JS, then you'll figure out the problem very quickly. But if you just always use semi-colons, then ASI is one fewer thing to have to worry about.
Re:Bling – the $ of jQuery without the jQuery by insin on Monday June 15, 2015 @08:00AM (#9718771)
Alternatively:

    console.log("woof")
    void function() { console.log("foo") }()
There really is only one ASI rule you need to remember [0] in practice: don't begin a new line with ( or [ if it's intended to be a new statement.

On this topic, it could so easily have been different; I found this interesting from Brendan Eich [1]:

> I wish I had made newlines more significant in JS back in those ten days in May, 1995. Then instead of ASI, we would be cursing the need to use infix operators at the ends of continued lines, or perhaps or brute-force parentheses, to force continuation onto a successive line. But that ship sailed almost 17 years ago.

> ...My two cents: be careful not to use ASI as if it gave JS significant newlines...

[0] this is technically a lie because everybody needs learn the other one whether they use semicolons or not: don't put a linebreak after the "return" keyword :) [1] https://brendaneich.com/2012/04/the-infernal-semicolon/

Re:Bling – the $ of jQuery without the jQuery by Bahamut on Monday June 15, 2015 @08:10AM (#9718807)
void blows up on Safari.
Re:Bling – the $ of jQuery without the jQuery by benihana on Monday June 15, 2015 @08:12AM (#9718813)
>There really is only one ASI rule you need to remember [0] in practice: don't begin a new line with ( or [ if it's intended to be a new statement.

Here's an even easier and clearer one: finish every statement with a semicolon.

Re:Bling – the $ of jQuery without the jQuery by swang on Monday June 15, 2015 @12:49PM (#9720520)

  function hello() {
    var x = 0;
    return
    {
      x: x,
      y: y
    };
  };
Just putting semicolons where you think it can work won't help you. If you write javascript, you must know what the asi does. So semicolons or no semicolons is completely irrelevant.
Re:Bling – the $ of jQuery without the jQuery by insin on Monday June 15, 2015 @08:33AM (#9718893)
This is precisely why I hate optional syntax, regardless of which way your preferences fall on this particular one.
Re:Bling – the $ of jQuery without the jQuery by bakabaka9 on Monday June 15, 2015 @11:34AM (#9719974)
Mindlessly inserting unnecessary syntax instead of learning the damn language. Hacker News should be proud of its lusers.
Re:Bling – the $ of jQuery without the jQuery by taco_emoji on Monday June 15, 2015 @12:18PM (#9720323)
I don't have a photographic memory. If "learning the language" means rote memorization of an inconsistent set of rules for how the fucking code is parsed, then you're goddamn right I'd rather mindlessly insert a character that obviates that.
Re:Bling – the $ of jQuery without the jQuery by atirip on Monday June 15, 2015 @07:37AM (#9718702)
A very simple rule - if line starts with '[' or '(', put semicolon before. Thats it.

Personally, if all is encapsulated inside self executing function, i put one semicolon before and one after it.

Re:Bling – the $ of jQuery without the jQuery by rtp on Monday June 15, 2015 @08:27AM (#9718872)
What about this?

    function v(x) {
     return
       x;
    }
    
    v("yeah");
returns undefined
Re:Bling – the $ of jQuery without the jQuery by atirip on Monday June 15, 2015 @09:07AM (#9719033)
What about it? Yes, it returns undefined. Has nothing to do with semicolons. You can add semicolon to every line of your example, or remove each and every one and still it returns undefined.
Re:Bling – the $ of jQuery without the jQuery by CatsoCatsoCatso on Monday June 15, 2015 @09:21AM (#9719105)
He's putting it in comparison to the previous example https://news.ycombinator.com/item?id=9718410

Newline matters / doesn't matter, the same with the semicolons.

Re:Bling – the $ of jQuery without the jQuery by bakabaka9 on Monday June 15, 2015 @06:14AM (#9718454)
They aren't needed at all. Semicolons in JS are optional.

Knowledge about how ASI works, on the other hand, is mandatory.

Re:Bling – the $ of jQuery without the jQuery by BinaryIdiot on Monday June 15, 2015 @10:38AM (#9719562)
> They aren't needed at all. Semicolons in JS are optional.

So they're not really optional. What they are is necessary and if omitted the engine attempts to put them in automatically. The rules for automatic semicolon insertion are in section 7.9 of the ECMAScript 5 standard (http://www.ecma-international.org/publications/files/ECMA-ST...).

This means you're omitting something that is required by the engine but not necessarily required in the syntax which boils down to you losing control of where semicolons end up possibly making your code do something you did not intend because you either misunderstood the rules of automatic semicolon insertion or there was a bug in the syntax parser being used.

Re:Bling – the $ of jQuery without the jQuery by bakabaka9 on Monday June 15, 2015 @11:24AM (#9719905)
You're only "losing control" if you don't know how ASI works, which is well-defined in the ECMAScript standard.

Bad programmers that can't understand ASI will be also confused by the floating point semantics, promises and other basic concepts — there are many ways of "making your code do something you did not intend" in JS, but that's no excuse for bashing language features.

Fact check: ASI is a fully legitimate part of the language (as in, it's in standard, it's documented, and supported across the board), so demonizing it is every bit as silly as e.g. deprecating C macros.

Learning the language helps with the the issues you outlined, while magical thinking (uguuu, semicolons good, no semicolons bad, uguuu) for the most part doesn't.

Re:Bling – the $ of jQuery without the jQuery by BinaryIdiot on Monday June 15, 2015 @11:37AM (#9719999)
> Bad programmers that can't understand ASI will be also confused by the floating point semantics, promises and other basic concepts — there are many ways of "making your code do something you did not intend" in JS, but that's no excuse for bashing language features.

This is a terrible attitude. There is no reason you may not understand every case in which a semi colon is automatically inserted but can still understand floating point, promises and other things. Automatic semicolon insertion isn't really a "basic concept"; it's a convenience for those who elide them and nothing more (the ECMAScript 5 standard even states this).

> Fact check: ASI is a fully legitimate part of the language (as in, it's in standard, it's documented, and supported across the board), so demonizing it is every bit as silly as e.g. deprecating C macros.

I'm not "demonizing" it but if you look at the standard the engine still requires semicolons it just fills them in for you if you miss them. This is vastly different than your deprecation of C macros example. Yes it's documented but it's non-obvious unless you've literally looked it up to learn it. I'm also not sure why you insisted on letting me know it's a "fully legitimate part of the language (as in, it's in standard, it's documented, and supported across the board" when I linked to the standard and stated exactly where to look for its definition...

> Learning the language helps with the the issues you outlined, while magical thinking (uguuu, semicolons good, no semicolons bad, uguuu) for the most part doesn't.

Sigh. Automatic semicolon insertion is only documented. There are no errors or warnings or other information. Many developers, who understand more advanced topics, may not even know about it. The language does nothing to help with this. It's far better to be more explicit than assuming everyone who looks at your code understands the full ECMAScript 5 standard.

Re:Bling – the $ of jQuery without the jQuery by taco_emoji on Monday June 15, 2015 @12:30PM (#9720391)
It's the same reason that, yes, you can put "var x" in an if-block, but it's bad practice because it misleads the reader (even one who knows that Javascript is function scoped) into thinking that x is block-scoped.
Re:Bling – the $ of jQuery without the jQuery by BinaryIdiot on Monday June 15, 2015 @02:03PM (#9720935)
I'm almost embarrassed to say it took me way to long to realize JavaScript wasn't block scoped.
Re:Bling – the $ of jQuery without the jQuery by taco_emoji on Monday June 15, 2015 @02:50PM (#9721232)
Same here! It's probably the norm, honestly.

Javascript never should've had C-like syntax if it was to have the feature set it does. Its syntax descends from a tradition of languages that have none of its actual features. Most other C-like languages have block scope[0]. So the syntax PLUS the terrible, awful, no-good very bad name lead you to believe it's more-or-less dynamic Java, when nothing could be further from the truth.

[0] ...and require declaration before usage, and have sensible 'this' semantics, and are class-based, and have function parameters that are more than just syntactic sugar, and have ACTUAL arrays, and...

Re:Bling – the $ of jQuery without the jQuery by mootothemax on Monday June 15, 2015 @06:23AM (#9718482)
They aren't needed at all. Semicolons in JS are optional.

Which becomes really annoying if you just want to quickly concatenate a few files together.

Sure, it's something that you can fix yourself; I'd consider it a common courtesy to include at least one, though.

Re:Bling – the $ of jQuery without the jQuery by bakabaka9 on Monday June 15, 2015 @11:31AM (#9719952)
In the worst case, you can do something along the lines of

    [file1, file2, file3].join(';\n')
albeit if you're using any kind of post-processing tool, like UglifyJS or Google's Closure compiler, then everything just works, as these tools actually parse JavaScript. There's usually no need to explicitly concatenate the code prior to minification or other processing.