+ 4

Where to use semicolons in JS?

While writing in JS frequently we have to use ; . Please can anyone say where to use or not to use ; .

13th May 2020, 12:35 AM
Md. Faheem Hossain
Md. Faheem Hossain - avatar
2 Answers
+ 3
It is a good practice to use semi-colon. The only situation you shouldn't use semi-colon is when you are chaining response callback of promise. from Morpheus : Maz I m glad you asked, I was waiting for this question :-) Actually if we write codes correctly then we dont need to use semicolons, but mistakes happen and the problem is ASI will cover your mistake without even throwing an error, thats y linters like JShint, JSlint throw warning on missing semicolons on those bad scenarios. and in general its a good habit to include them, regarding those statements here are the 3 articles quick: https://news.codecademy.com/your-guide-to-semicolons-in-javascript/ in depth : http://www.bradoncode.com/blog/2015/08/26/javascript-semi-colon-insertion/ official: http://www.ecma-international.org/ecma-262/6.0/index.html#sec-automatic-semicolon-insertion (see chapter 11.9) source: https://www.sololearn.com/post/36048/?ref=app https://code.sololearn.com/Wyr76080kKxS/?ref=app
13th May 2020, 12:36 AM
Gordon
Gordon - avatar
+ 3
The JavaScript parser will automatically add a semicolon when, during the parsing of the source code, it finds these particular situations: 1.when the next line starts with code that breaks the current one (code can spawn on multiple lines) 2.when the next line starts with a }, closing the current block 3.when the end of the source code file is reached 4.when there is a return statement on its own line 5.when there is a break statement on its own line 6.when there is a throw statement on its own line 7.when there is a continue statement on its own line
13th May 2020, 12:58 AM
Satyam Patel
Satyam Patel - avatar