惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Webroot Blog
Webroot Blog
AI
AI
P
Palo Alto Networks Blog
Attack and Defense Labs
Attack and Defense Labs
WordPress大学
WordPress大学
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
Spread Privacy
Spread Privacy
T
Tor Project blog
罗磊的独立博客
小众软件
小众软件
S
Security Affairs
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Apple Machine Learning Research
Apple Machine Learning Research
T
Threatpost
NISL@THU
NISL@THU
博客园_首页
PCI Perspectives
PCI Perspectives
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
N
News and Events Feed by Topic
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Forbes - Security
Forbes - Security
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
L
LINUX DO - 热门话题
T
Threat Research - Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Security Latest
Security Latest
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Cloudflare Blog
A
About on SuperTechFans
爱范儿
爱范儿
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
TaoSecurity Blog
TaoSecurity Blog
宝玉的分享
宝玉的分享
G
GRAHAM CLULEY
雷峰网
雷峰网
F
Full Disclosure
I
Intezer
Cloudbric
Cloudbric
博客园 - 三生石上(FineUI控件)
U
Unit 42

Kent C. Dodds Blog

Implementing Hybrid Semantic + Lexical Search Simplifying Containers with Cloudflare Sandboxes Migrating to Workspaces and Nx Offloading FFmpeg with Cloudflare Building Semantic Search on my Content Helping YOU ask ME questions with AI How I used Cursor to Migrate Frameworks The Dow's Start on the Covenant Path 2025 in Review The next chapter: EpicAI.pro AI is taking your job How I increased my visibility Launching Epic Web 2023 in Review Stop Being a Junior RSC with Dan Abramov and Joe Savona Live Stream Fixing a Memory Leak in a Production Node.js App 2022 in Review My Car Accident I Migrated from a Postgres Cluster to Distributed SQLite with LiteFS I'm building EpicWeb.dev A review of my time at Remix Remix: The Yang to React's Yin How I help you build better websites Why I Love Remix The State Initializer Pattern How to React ⚛️ Get a catch block error message with TypeScript Building an awesome image loading experience How Remix makes CSS clashes predictable Introducing the new kentcdodds.com How I built a modern website in 2021 How to use React Context effectively Static vs Unit vs Integration vs E2E Testing for Frontend Apps The Testing Trophy and Testing Classifications Array reduce vs chaining vs for loop Don't Solve Problems, Eliminate Them Super Simple Start to Remix Super Simple Start to ESModules in Node.js JavaScript Pass By Value Function Parameters How to write a Constrained Identity Function (CIF) in TypeScript How to optimize your context value How to write a React Component in TypeScript TypeScript Function Syntaxes Listify a JavaScript Array Build vs Buy: Component Libraries edition Using fetch with TypeScript Wrapping React.useState with TypeScript Define function overload types with TypeScript 2020 in Review Business and Engineering alignment Hi, thanks for reaching out to me 👋 useEffect vs useLayoutEffect Super simple start to Firebase functions Super simple start to Netlify functions Super Simple Start to css variables Favor Progress Over Pride in Open Source Testing Implementation Details How getting into Open Source has been awesome for me useState lazy initialization and function updates Use ternaries rather than && in JSX Application State Management with React Use react-error-boundary to handle errors in React JavaScript to Know for React How I structure Express apps What open source project should I contribute to? When I follow TDD AHA Programming 💡 How I Record Educational Videos Should I write a test or fix a bug? Stop mocking fetch Intentional Career Building Improve test error messages of your abstractions Tracing user interactions with React Eliminate an entire category of bugs with a few simple tools Common mistakes with React Testing Library Super Simple Start to React Stop using client-side route redirects The State Reducer Pattern with React Hooks Function forms Replace axios with a simple custom fetch wrapper How to test custom React hooks React Production Performance Monitoring Should I useState or useReducer? Stop using isLoading booleans Make Your Test Fail Make your own DevTools An Argument for Automation Fix the "not wrapped in act(...)" warning Super Simple Start to ESModules in the Browser Implementing a simple state machine library in JavaScript 2010s Decade in Review Why users care about how you write code Why I avoid nesting closures Don't call a React function component Why your team needs TestingJavaScript.com Inversion of Control Understanding React's key prop How to Enable React Concurrent Mode Profile a React App for Performance
Semicolons in JavaScript: A preference
2015-11-16 · via Kent C. Dodds Blog

Update:

Now that I use prettier, it's really a matter of what you like to look at (because prettier means that I don't have to type them at all). I prefer the way code looks without semicolons so… :) I just use eslint-config-prettier and let prettier deal with it.


Semicolons in JavaScript has got to be one of the worst bikeshedded topics of all time (right after spaces vs. tabs... 2 spaces please). Here are three resources on the subject of why semicolons are not necessary. Here's some criteria that need to be in place before I will sanction omitting semicolons in a project.

Compilation and/or Uglification

The first thing that you need to know is something called Automatic Semicolon Insertion (ASI). It's the "feature" that allows us to even have this conversation. Read up on that if you're not familiar. You should not rely on ASI. It's a really bad idea for many reasons.

The problems with relying on ASI go away when you compile or minify your code (depending on your technology). For example, compiling with Babel will add the semicolons back and uglifying with terser will too.

So for me to say you're good to go on omitting semicolons in your source code, you first need to make sure that whatever you end up shipping to production (whether browser or node) has the semicolons added back.

Linting the bad parts

There are a few gotchas with ASI. However, if you are using ESLint (which you should) and you enable the no-unexpected-multiline rule, then you're safe. Just make sure that your build pipeline will fail if that rule is broken because most assuredly your app will!

Why omit semicolons anyway?

With these things in place, this is no longer a discussion about what works and what doesn't but becomes a simple matter of preference.

Omitting semicolons is a matter of preference

So why do I prefer to not have semicolons? It's not just that I have a broken right pinky (though sometimes it gets hurt) or I like typing one less character per line. It's simply because I don't like my linter/editor telling me I need to add something that is not necessary.

Also, I like to stay focused on the problem, not worrying about adding or removing something that doesn't really matter in the end. And since I've started omitting semicolons (and gotten used to how ugly it looks at first) I actually feel like it leaves my code looking cleaner (you've just gotta be untrained to think that you need semicolons).

Why should you use semicolons?

One argument I've heard for using semicolons is that without them, it's harder for newcomers to understand the code. I suppose it's possible, but that hasn't been my experience with newcomers I've taught. For me I don't feel like my code has gotten any less clear, maintainable, or readable since I removed semicolons from it. And now I don't have to even think about it.


Conclusion

If you don't compile/uglify and lint your code properly then I do not recommend you omit semicolons in your code (it's not a matter of preference in this case, it's simply the proper way to write JavaScript). I definitely would recommend that you get these things in place (but that's another blogpost). If you do have these things in place, then that's great! You can make the choice based on preference! Catch you on the twitters!