JavaScript Semicolons Save the Day in React

Recently I was learning React and something really unexpected happened. My code was syntactically correct, logically sound, and still crashed at runtime. The culprit was not state, not hooks, not JSX, but a single missing semicolon. That moment forced me to rethink how “optional” some JavaScript rules really are. In JavaScript, semicolons are optional because of Automatic Semicolon Insertion. But optional does not mean safe. JavaScript does not respect line breaks the way we visually expect it to. When a self calling function is written immediately after another statement, the engine may try to chain them together, leading to errors that feel completely out of context. Here is a real example that explains the problem clearly: const data = [1, 2, 3] (function () { console.log("Running IIFE") })() To us, these are two separate blocks. To JavaScript, this can mean "call data as a function". The result is a confusing TypeError: data is not a function. Nothing is wrong with React or your logic. The issue is simply that JavaScript was not told where to stop. Now watch how one intentional semicolon changes everything: const data = [1, 2, 3] ;(function () { console.log("Running IIFE safely") })() That single ";" removes all ambiguity. It makes your code explicit, predictable, and safe. This is why many experienced developers always prefix IIFEs with a semicolon, even in projects where semicolons are mostly avoided. The takeaway is simple. In large React applications where files are bundled and executed together, small assumptions can turn into big bugs. A semicolon before a self calling function is not a style choice. It is a protective habit that saves time, confusion, and production issues. #JavaScript #React #Learning #UpSkilling #WebDevelopment #CodingLife #Developers

Boss trust me, use typescript 10 times better

Like
Reply

To view or add a comment, sign in

Explore content categories