mirror of
				https://github.com/goldbergyoni/nodebestpractices.git
				synced 2025-10-31 09:38:39 +08:00 
			
		
		
		
	Rename 3.4 Use Semicolon to proper statement separation
Fixes #26. I hope I integrated all feedback from this issue, while leaving the core message intact: People should be aware how statements are separated or linked together, and that there are cases where ASI and/or semicolon do not help, if statement termination is not understood. This also leaves room for the reader to prefer semicolons or not.
This commit is contained in:
		
							
								
								
									
										34
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								README.md
									
									
									
									
									
								
							| @ -271,28 +271,48 @@ function someFunction() | ||||
|  | ||||
| <br/><br/> | ||||
|  | ||||
| ## ![✔] 3.4 Don't Forget the Semicolon | ||||
| ## ![✔] 3.4 Separate your statements properly | ||||
|  | ||||
| **TL;DR:** While not unanimously agreed upon, it is still recommended to put a semicolon at the end of each statement. This will make your code more readable and explicit to other developers who read it | ||||
| No matter if you use semicolons or not to separate your statements, knowing the common pitfalls of inproper linebreak or automatic semicolon insertion, will help you to eliminate regular syntax errors. | ||||
|  | ||||
| **Otherwise:** As seen in the previous section, JavaScript's interpreter automatically adds a semicolon at the end of a statement if there isn't one, or considers a statement as not ended where it should, which might lead to some undesired results | ||||
| **TL;DR:** Use ESLint to make you aware about separation concerns. Prettier or Standardjs can automatically resolve these issues. | ||||
|  | ||||
| **Otherwise:** As seen in the previous section, JavaScript's interpreter automatically adds a semicolon at the end of a statement if there isn't one, or considers a statement as not ended where it should, which might lead to some undesired results. You can use assignments and avoid using immediate invoked function expressions to prevent most of unexpected errors. | ||||
|  | ||||
| ### Code example | ||||
|  | ||||
| ```javascript | ||||
| // Do | ||||
| const count = 2; | ||||
| (function doSomething() { | ||||
|   // do something amazing | ||||
| }()); | ||||
| function doThing() { | ||||
|     // ... | ||||
| } | ||||
|  | ||||
| doThing() | ||||
|  | ||||
| // Do | ||||
|  | ||||
| const items = [1, 2, 3] | ||||
| items.forEach(console.log) | ||||
|  | ||||
| // Avoid — throws exception | ||||
| const m = new Map() | ||||
| const a = [1,2,3] | ||||
| [...m.values()].forEach(console.log) | ||||
| > [...m.values()].forEach(console.log) | ||||
| >  ^^^ | ||||
| > SyntaxError: Unexpected token ... | ||||
|  | ||||
| // Avoid — throws exception | ||||
| const count = 2 // it tries to run 2(), but 2 is not a function | ||||
| (function doSomething() { | ||||
|   // do something amazing | ||||
| }()) | ||||
| // put a semicolon before the immediate invoked function, after the const definition, save the return value of the anonymous function to a variable or avoid IIFEs alltogether | ||||
| ``` | ||||
|  | ||||
| 🔗 [**Read more:** "Semi ESLint rule"](https://eslint.org/docs/rules/semi) | ||||
| 🔗 [**Read more:** "No unexpected multiline ESLint rule"](https://eslint.org/docs/rules/no-unexpected-multiline) | ||||
|  | ||||
| <br/><br/> | ||||
|  | ||||
| ## ![✔] 3.5 Name your functions | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Martin Muzatko
					Martin Muzatko