semicolon or no semicolon for javascript

So I've been doing some research on my own and now dabling/learning JS. One of the things I notice is this whole semicolon vs no semicolon. From what I'm seeing almost all other language use semicolon so my question here is. Do you need to use semicolon or can you create an entire application without a semicolon. Whats the correct way to write JS code or i should say whats the job ready habit I should take on from now when it comes to writing JS
13 Replies
b1mind
b1mind17mo ago
this is totally a preference also you have linting so honestly its kinda a mute point. I don't like semi you like semi, we can just lint to one standard and each writes it how we want and let the linter add/remove on what is decided. This can be done locally or even on repo with a precommit hook.
Tenkes
Tenkes17mo ago
Semicolons are optional in JS. If you don't add them JS will do it during passing. So yes, you can write whole application without semis
dellannie
dellannieOP17mo ago
@b1mind I didnt even know about linter but thanks for that input @.tenkes thanks for the input as well. I was making sure I'm not learning the wrong thing lol
Joao
Joao17mo ago
They are not optional, technically. But yes JavaScript will add them for you as it can infer most of the time, but there are some (extremely rare) gotchas. Personally, I like to have them... just a habit. Linter is a program that scans your source code as you write and gives warnings based on a set of rules. For example you and I can agree on not using semicolons, limit the length of each line to 80 characters, and so on... the linter will let us know if we break one of these rules.
dellannie
dellannieOP17mo ago
@joao6246 thats what I keep seeing. Some folks like them some don't
Joao
Joao17mo ago
Then, there's a formatter that will apply those rules automatically.
dellannie
dellannieOP17mo ago
so is linter like a plugin or no
Joao
Joao17mo ago
Yeah... just do what you like it's not really relevant. In VSCode there are plugins for it. But a linter itself is just a program like any other. Search for ESLint (linter) and Prettier (formatter). The difference is that the linter simply lets you know about the rules you broke, which you can of course configure, and the formatter will apply them i.e., it will actually modify your source code.
b1mind
b1mind17mo ago
you can run the --fix flag too though so in a pre commit hook the repo could just follow/format linting rules while locally you do you. least as I understand it... I just do me so 🤷‍♂️ no semi life ❤️
Joao
Joao17mo ago
That's the way to go 😎
dellannie
dellannieOP17mo ago
I use prettier a lot lol i like that formatter
Joao
Joao17mo ago
Think of it this way, a formatter wouldn't know how to format the code without some rules, right? That's the linter's job. Prettier by itself doesn't do anything, it needs somewhere too look at to know what to do. I believe it comes with some default rules, or maybe it's VSCode that applies these rules once you install the plugin, I don't remember exactly. But yeah, these two things work together.
dellannie
dellannieOP17mo ago
okay okay. Now i see. thanks for breaking that down @b1mind thanks as well.
Want results from more Discord servers?
Add your server