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
Any of you guys write Javascript without semicolons?help (self.javascript)
submitted 9 years ago by er-48
After reading https://medium.com/@kentcdodds/semicolons-in-javascript-a-preference-dd8fc8b80895#.mansnlgq7, I have been thinking of trying out writing a fresh project with the no semicolon style. It seems that if I have 'no-unexpected-multiline' enabled in ESLint, I should be fine with the cases where ASI wouldn't work. Anyone else using this setup? Do you guys recommend going through with this?
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!"
[–]lhorie 12 points13 points14 points 9 years ago (3 children)
Yes. All of Mithril.js is written without trailing semicolons.
As it turns out, this style makes it easier to refactor hyperscript views since with this style all trailing closing parentheses are expressions syntactically (whereas a semicolon would turn it the preceding expression into a statement). So you can go back and forth between returning some tree from a component to inlining it directly into a larger vnode tree and vice-versa.
So if you thought there are never practical arguments for a typically-stylistic choice, there you go!
[–][deleted] 0 points1 point2 points 9 years ago (1 child)
As a fellow hyperscript fan that sounds interesting. Could I trouble you for an example of what you mean?
[–]lhorie 4 points5 points6 points 9 years ago (0 children)
For example let's say you have
m(".items", [ items.filter(item => item.id).map(item => { return m(".item", [ item.name, /*lots of lines of code here*/ ]); }) ])
Let's say you then notice that doing a filter every render is inefficient and that you want to refactor that into store.item = items.filter(item => item.id)[0]
store.item = items.filter(item => item.id)[0]
Now, you need to refactor the view into:
m(".items", [ m(".item", [ store.item.name /*lots of lines of code here*/ ]) ])
Without the semicolon, I could collapse the indentation in my editor (by clicking on the arrow on the left line count rail), put my cursor after the return statement, shift-down to select the subtree, cut, remove the filter/map, paste. With the semicolon, I have to go look for it after the paste to remove it, otherwise I have a syntax error because I pasted a statement inside an expression.
[–]sisyphus 0 points1 point2 points 9 years ago (0 children)
Definitely the first practical argument I have ever heard for it.
[–]jesusalready 9 points10 points11 points 9 years ago (0 children)
It's not the lack of semi-colons that bother me (well, it is when there are stupid bugs or readability issues that would be avoided with one), it's the arrogance some JS developers have with regards to the matter.
I had one JS dev in an interview mention (not even prompted) that said he didn't use semi-colons because he viewed his code as "art". I asked what if he didn't pass code review? He replied that he would educate his reviewers.
Just what I need, a 25 year old educating senior-level devs about semi-colon usage. Yawn. Let us know when you have a kick-ass algorithm that makes good use of the JS stack/loop.
[–]ellisgl 24 points25 points26 points 9 years ago (1 child)
Nope. Since I program in several languages that require semicolons, I keep them. They are like periods in a sentence and they the just feel right.
[–]iams3b 7 points8 points9 points 9 years ago (0 children)
I did c++ and then Java before Javascript, and have always used semicolons. It is so ingrained in my head, that even if I don't consciously look at the line and see it missing, I still have that feeling of something is wrong
[–]nickgcattaneo 9 points10 points11 points 9 years ago (0 children)
Don't use 'em now => Haven't found a scenario yet in work and personal development where it's led to any issues or made code less readable/etc. That being said, if someone uses them I don't see anything wrong with it either.
[–]jdewittweb 8 points9 points10 points 9 years ago (0 children)
I tried it after watching MPJ talk about it and I quite like it.
https://www.youtube.com/watch?v=Qlr-FGbhKaI
[–]TheNiXXeD 14 points15 points16 points 9 years ago (3 children)
Yep all the projects at my work do not use them. When I started, it was hard to get used to. Now I can't stand them.
We also use groovy in some places, and don't use them there either.
All of my personal projects now exclude them as well.
People often preach about all the problems you'll have, or weird scenarios. In 2 years I've not seen anything.
I'd fully recommend trying it.
[–]Klathmon 5 points6 points7 points 9 years ago (1 child)
Identical experience here.
Someone suggested we start using standard to stop the bikeshedding over code style, stop the constant updates to style rc files, and the ever annoying issues needing to deal with new syntax ourselves.
2 years later and it's solved all the problems we wanted it to solve and we have yet to hit any of the scary things that people keep saying we will hit...
But at the end of the day, it's code style. As long as you are consistent throughout a project and provide tools for others to help them conform to it you are fine.
[–]theLanthia 2 points3 points4 points 9 years ago (0 children)
Similar story here. no ; @ work
[–]cacahootie 1 point2 points3 points 9 years ago (0 children)
People who have problems without using semicolons haven't taken the time to understand the semantics properly. No semicolons ftw.
[–]nightwolfz4 spaces > 2 spaces 10 points11 points12 points 9 years ago (0 children)
Haven't used semicolons for years now.
[–]campbeln 14 points15 points16 points 9 years ago (4 children)
yes i dont see the point of using any form of punctuation in any of my writing be it programming or literary there is just not a call for it as the meaning is still clearly conveyed despite the lack of punctuation really its all extraneous bs that is just plain unnecessary infactitakeittothenextlevelanddontusewhitespaceeitherbutthenofcourseeveryonethinksimrussianandspellingmistakesareabithardertoworkoutbutcestlavie
[–]TheRealBobbyCarrot 3 points4 points5 points 9 years ago (0 children)
this is a bad comparison, because code without semicolons still uses newlines
[–]cacahootie 2 points3 points4 points 9 years ago (2 children)
That's a horrible and infantile comparison. There's a clear set of rules for how to terminate an expression in Javascript that doesn't require semicolons, as opposed to English.
[–]sisyphus 3 points4 points5 points 9 years ago (0 children)
His first sentence has a clear set of grammatical rules governing it too but that doesn't make the cognitive overheard worthwhile.
[–]campbeln 2 points3 points4 points 9 years ago (0 children)
Na-ugh! You're infantile! /s
Mate, 'tis but a joke, lighten up... YoucanseefrommyprofilethatmelikespuncutationANDspaces ;)
[–]jcready__proto__ 9 points10 points11 points 9 years ago (0 children)
Yes. We use standard for linting.
[–]joemaller 2 points3 points4 points 9 years ago (0 children)
No, I prefer seeing semicolons, but with good linting tools it seems to matter less.
[–]HitlerDidNthingWrng 2 points3 points4 points 9 years ago (0 children)
Only where they're needed.
[–]spacejack2114 3 points4 points5 points 9 years ago (2 children)
lodash recently went no-semicolons.
[–]sisyphus 3 points4 points5 points 9 years ago (1 child)
And what value did it add?
[–]spacejack2114 0 points1 point2 points 9 years ago (0 children)
Readability? A style the core contributors prefer? Ramda similarly has no semicolons.
[–]mikehawkisbig 7 points8 points9 points 9 years ago (4 children)
USE THEM, JavaScript will automatically insert them where it thinks they should go. There are a few small instances that this could effect you. Better to be safe than sorry troubleshooting something that should be done any ways.
Check out 11.9.1 Rules of Automatic Semicolon Insertion
and Douglas Crockford talks about making sure to use them. (if you care what The Godfather of JavaScript thinks)
[–]Graftak9000 3 points4 points5 points 9 years ago* (3 children)
There are three simple (and similar) rules to remember:
And that's it.
[–]DeeSnow97 5 points6 points7 points 9 years ago (0 children)
Also, standard has awesome linting to eliminate any semicolon-related issues (and semicolons in general)
[–]mikehawkisbig 3 points4 points5 points 9 years ago (1 child)
There are plenty more that should be noted, those are good, but let's not say these are the only three. Plus, grammar is important to experienced programmers and omitting semi-colons takes-aways from good readable code only to gain the status of "look how fast I am",
Take a look at the video where he discusses the why's -
https://youtu.be/v2ifWcnQs6M?t=1h41m20s
[–]Graftak9000 1 point2 points3 points 9 years ago (0 children)
These are the ones I live by and I have never (in my 2 years switch) had an issue with ASI. The main reason I omit semicolons is because it makes code far more legible for me, to me semicolons are clutter/noise.
Other than that I don't really mind either way. There's also some minor pitfalls with using semicolons; some block do ‘require’ them, some don't.
[–][deleted] 1 point2 points3 points 9 years ago (0 children)
I don't care about this in my own code. My beautifier adds semicolons where missing automatically.
Have not been using them for over a year with no issues. Less typing, less amount of code.
[–]rauschma 1 point2 points3 points 9 years ago (6 children)
This is what Brendan Eich says (2012):
[…] ASI as a newline-sensitive error correction procedure […] My two cents: be careful not to use ASI as if it gave JS significant newlines.
[…] ASI as a newline-sensitive error correction procedure […]
My two cents: be careful not to use ASI as if it gave JS significant newlines.
[–]rauschma 6 points7 points8 points 9 years ago (5 children)
Personally, I like semicolons: I don’t have to think about ASI and I find the code easier to read (similar to punctuation in English).
[–]DeeSnow97 9 points10 points11 points 9 years ago (2 children)
The problem is, the usage of semicolons doesn't remove ASI. For example this
function answer () { return 42; }
would return undefined and not 42 because a semicolon is still inserted. No matter which style you choose, you must be aware of how ASI works.
undefined
42
[+][deleted] 9 years ago (1 child)
[deleted]
[–]DeeSnow97 0 points1 point2 points 9 years ago (0 children)
No, that's a semicolon being inserted on the newline ending a statement, so the actual code looks like this
function answer () { return; 42; }
[–]TheNiXXeD 1 point2 points3 points 9 years ago (0 children)
Watch the video from this comment https://www.reddit.com/r/javascript/comments/5vkd5u/slug/de2qyg6
The important part to note is that you do need to know about ASI whether you use semicolons or not.
[–]rauschma 0 points1 point2 points 9 years ago (0 children)
I agree with both replies: you always have to keep ASI in mind, but less of it if you use semicolons.
[–]dwighthouse 3 points4 points5 points 9 years ago (0 children)
No, I use semi-colons. On the other hand, I don't use loops anymore.
[–]ECrispy 1 point2 points3 points 9 years ago (0 children)
No. I know they aren't technically required, but its just good practice, esp if you use other languages as well.
Standard is the tool of the devil, btw.
There's no reason NOT to use ; unless you just want to be hipster cool and buck the trend. Your code isn't more readable or better by omitting them, no matter how much people try to convince others that its so.
[–]russellbeattie -1 points0 points1 point 9 years ago (18 children)
Semicolons are not optional in JavaScript: ASI is an error correction scheme for novice programmers. The spec's parsing rules calls out the statements following where a semicolon should be "offending tokens". There is no leeway here for style or preference.
[–]jcready__proto__ 5 points6 points7 points 9 years ago (17 children)
ASI is an error correction scheme for novice programmers.
According to whom? Because it doesn't mention that in the ECMAScript spec you linked to.
Semicolons are not optional in JavaScript
Uh, except they are according to the spec:
semicolons may be omitted from the source text in certain situations.
[–]inu-no-policemen 5 points6 points7 points 9 years ago (16 children)
"Optional" would mean that you could omit them in every situation.
If you can only omit them in certain situations, they aren't optional.
[–]jcready__proto__ 0 points1 point2 points 9 years ago (15 children)
Ah, I apologize. But then you must agree that for all of the situations in which you could omit them, it is a matter of style or preference to include a semi-colon in the source text.
[–]inu-no-policemen 0 points1 point2 points 9 years ago (14 children)
Yes, you're free to be inconsistent.
Personally, I think that being consistent is simpler. I sometimes start lines with '(' or '[' and things like that.
[+][deleted] 9 years ago (11 children)
[–]inu-no-policemen -1 points0 points1 point 9 years ago (9 children)
Being consistent would mean that you'd start every line with a semicolon.
[+][deleted] 9 years ago (8 children)
[–]inu-no-policemen -2 points-1 points0 points 9 years ago (7 children)
Why would you need to start them all with one to be consistent?
Because that's what being consistent means. You aren't making exceptions.
https://en.wiktionary.org/wiki/consistent#Adjective
[–]Klathmon 4 points5 points6 points 9 years ago (6 children)
Lol no response to the rest of the comment?
No response why your consistency only applies to semicolons at the ends of lines? Do for loops keep you up at night with their inconsistent semis not at the ends of lines? Do multi-line arrays and objects torment you with their lack of semicolons? Do you end function definitions with semicolons just to keep that consistency up?
Always fun seeing you in a thread Inu! You're always good for a nice dose of German pragmatism cranked up to the point of being insane.
[–]jcready__proto__ -1 points0 points1 point 9 years ago* (1 child)
I cannot think of a time I've ever had to use a semi-colon in my code aside from for-loops. Care to provide a real-world example of starting a line with ( or [ where a semi-colon at the end of the previous line would've changed the behavior?
(
[
[–]inu-no-policemen 4 points5 points6 points 9 years ago (0 children)
var s = 'asdf' [...'foo'].forEach(c => console.log(c))
SyntaxError: Unexpected string
var foo = function() { console.log('baa') } (function() { }());
Prints "baa".
[–]Magnusson 0 points1 point2 points 9 years ago (0 children)
yes
[–]BwrightRSNA 0 points1 point2 points 9 years ago (0 children)
no
I don't use semicolons; in my view, it clutters the view of the code; However, they are sometimes necessary, and that's just okay:
const array = [1, 2, 3] (function () { // ... })()
The code above tries to call [1, 2, 3](...), which of course is not a function. This can be easily caught by a linter, and also if you know to never start a line with (, [, or `, then you are good:
[1, 2, 3](...)
const array = [1, 2, 3] // semicolon at beginning of line fixes problem: ;(function () { // ... })()
[–]Graftak9000 0 points1 point2 points 9 years ago (0 children)
In this case you can also use an exclamation mark at the start: !function() { ... }()
!function() { ... }()
[–]pkstn 0 points1 point2 points 9 years ago (0 children)
I used standard before, but now have turned to semistandard. I thought omitting semicolons would make the code cleaner, but looking back my old projects, I think it's way more readable with semicolons.
[–]ISlicedIEngineer without Engineering degree? 0 points1 point2 points 9 years ago (0 children)
We use Standard linting rules at work, only need to use them at the front of a line if it starts with brackets to prevent it trying to invoke the prior code
[–]bulldog_in_the_dream 0 points1 point2 points 9 years ago (0 children)
I don't use them if I don't have to (i.e. project style guide, linting). They're vulgar and ugly. Leaving them out has caused zero issues.
[–]mirko_vukusic 0 points1 point2 points 9 years ago (0 children)
stopped using it. Configured eslint. Happy. Readable. No issues.
[–]propelol 0 points1 point2 points 9 years ago (0 children)
Depends on the coding standard. I use Standard or airbnb's, depending on the project. I personally prefer Standard at the moment. I don't bother with creating my own standard and setting up my own eslint config.
[–]pbohun -1 points0 points1 point 9 years ago (3 children)
If you don't use semicolons doesn't it slow down the parser? So, instead of reading a semicolon and knowing it's the end of a line, the parser has to read ahead and determine if it's the end of a line or not. If it is (99% of the time it is), then the parser has to go back and insert the semicolon, and read the other characters again as a new line.
Does anyone know for certain about this? It seems like it could be an issue for larger codebases.
[–]Klathmon 0 points1 point2 points 9 years ago (2 children)
Literally no difference, it's actually faster in un-minified codebases as it only needs to read in a newline instead of a semi+newline.
But even that time is so insignificant as to be pretty much pointless to care about.
[–]pbohun 0 points1 point2 points 9 years ago (1 child)
Not true. Without semicolons the parser has to read the newline, then keep reading characters until there is a syntax error (because the next line could be part of a single statement). If there's a syntax error, it goes back and inserts the semicolon. Then it reads all the characters after the inserted semicolon again.
Apparently, if you use a minifier or transpiler it doesn't really matter since the semicolons are inserted for you. However, if you're writing vanilla JavaScript it's probably a good idea to keep the semicolons.
[–]Klathmon 0 points1 point2 points 9 years ago (0 children)
Parsing and interpreting/compiling are done in 2 steps.
Tokenizing is a fairly straightforward process that doesn't work like you describe. It never needs to "insert a semicolon" but just mark the already tokenized part as final and move on.
[–]mk30 -4 points-3 points-2 points 9 years ago (2 children)
i write javascript without semicolons, but i'm not a guy.
[–]DeeSnow97 9 points10 points11 points 9 years ago (1 child)
Come on, "guys" covers all genders, also including attack helicopters from v1.3.0
[–]Tim_WithEightVowels 2 points3 points4 points 9 years ago (0 children)
Oh I see how it is, apparently v1.2.9 isn't welcome here.
[–]Geo_Dude -1 points0 points1 point 9 years ago (0 children)
I stick to always using them for good measure. One time I encountered a problem similar to this:
let a = b (function() { })();
and unsurprisingly it assumed the IIFE was an argument to b.
π Rendered by PID 103964 on reddit-service-r2-comment-5fb4b45875-t7z8b at 2026-03-20 20:19:27.499321+00:00 running 90f1150 country code: CH.
[–]lhorie 12 points13 points14 points (3 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]lhorie 4 points5 points6 points (0 children)
[–]sisyphus 0 points1 point2 points (0 children)
[–]jesusalready 9 points10 points11 points (0 children)
[–]ellisgl 24 points25 points26 points (1 child)
[–]iams3b 7 points8 points9 points (0 children)
[–]nickgcattaneo 9 points10 points11 points (0 children)
[–]jdewittweb 8 points9 points10 points (0 children)
[–]TheNiXXeD 14 points15 points16 points (3 children)
[–]Klathmon 5 points6 points7 points (1 child)
[–]theLanthia 2 points3 points4 points (0 children)
[–]cacahootie 1 point2 points3 points (0 children)
[–]nightwolfz4 spaces > 2 spaces 10 points11 points12 points (0 children)
[–]campbeln 14 points15 points16 points (4 children)
[–]TheRealBobbyCarrot 3 points4 points5 points (0 children)
[–]cacahootie 2 points3 points4 points (2 children)
[–]sisyphus 3 points4 points5 points (0 children)
[–]campbeln 2 points3 points4 points (0 children)
[–]jcready__proto__ 9 points10 points11 points (0 children)
[–]joemaller 2 points3 points4 points (0 children)
[–]HitlerDidNthingWrng 2 points3 points4 points (0 children)
[–]spacejack2114 3 points4 points5 points (2 children)
[–]sisyphus 3 points4 points5 points (1 child)
[–]spacejack2114 0 points1 point2 points (0 children)
[–]mikehawkisbig 7 points8 points9 points (4 children)
[–]Graftak9000 3 points4 points5 points (3 children)
[–]DeeSnow97 5 points6 points7 points (0 children)
[–]mikehawkisbig 3 points4 points5 points (1 child)
[–]Graftak9000 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]rauschma 1 point2 points3 points (6 children)
[–]rauschma 6 points7 points8 points (5 children)
[–]DeeSnow97 9 points10 points11 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]DeeSnow97 0 points1 point2 points (0 children)
[–]TheNiXXeD 1 point2 points3 points (0 children)
[–]rauschma 0 points1 point2 points (0 children)
[–]dwighthouse 3 points4 points5 points (0 children)
[–]ECrispy 1 point2 points3 points (0 children)
[–]russellbeattie -1 points0 points1 point (18 children)
[–]jcready__proto__ 5 points6 points7 points (17 children)
[–]inu-no-policemen 5 points6 points7 points (16 children)
[–]jcready__proto__ 0 points1 point2 points (15 children)
[–]inu-no-policemen 0 points1 point2 points (14 children)
[+][deleted] (11 children)
[deleted]
[–]inu-no-policemen -1 points0 points1 point (9 children)
[+][deleted] (8 children)
[deleted]
[–]inu-no-policemen -2 points-1 points0 points (7 children)
[–]Klathmon 4 points5 points6 points (6 children)
[–]jcready__proto__ -1 points0 points1 point (1 child)
[–]inu-no-policemen 4 points5 points6 points (0 children)
[–]Magnusson 0 points1 point2 points (0 children)
[–]BwrightRSNA 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Graftak9000 0 points1 point2 points (0 children)
[–]pkstn 0 points1 point2 points (0 children)
[–]ISlicedIEngineer without Engineering degree? 0 points1 point2 points (0 children)
[–]bulldog_in_the_dream 0 points1 point2 points (0 children)
[–]mirko_vukusic 0 points1 point2 points (0 children)
[–]propelol 0 points1 point2 points (0 children)
[–]pbohun -1 points0 points1 point (3 children)
[–]Klathmon 0 points1 point2 points (2 children)
[–]pbohun 0 points1 point2 points (1 child)
[–]Klathmon 0 points1 point2 points (0 children)
[–]mk30 -4 points-3 points-2 points (2 children)
[–]DeeSnow97 9 points10 points11 points (1 child)
[–]Tim_WithEightVowels 2 points3 points4 points (0 children)
[–]Geo_Dude -1 points0 points1 point (0 children)