+ 2

Is semi colon optional in JavaScript? If so is it best practice to use it and why?

1st May 2019, 10:50 AM
Hamdan
Hamdan - avatar
2 Answers
+ 12
JavaScript does not strictly require semicolons. When there is a place where a semicolon was needed, it adds it behind the scenes. The process that does this is called Automatic Semicolon Insertion.But without semicolons the code needs more time to interpret
1st May 2019, 12:46 PM
Mohd Abdul Sameer
Mohd Abdul Sameer - avatar
+ 4
IMHO, logically, without semicolons the parser may need more time in trying and parsing the script, as there are no definite statement separator being used. By using the semicolon we make it clear for the parser where statements ends and begins. And being clear, it's easier to parse and parsing process takes less time. From the sources below, I also found that apart from possible extra parsing time, scripts without semicolons may not run as intended after they are compressed (minified), some commands even may cause error without having the script compressed. https://codeburst.io/why-i-prefer-to-use-semicolon-in-javascript-f00c303547 http://www.bradoncode.com/blog/2015/08/26/javascript-semi-colon-insertion/ https://news.codecademy.com/your-guide-to-semicolons-in-javascript/ https://stackoverflow.com/questions/444080/do-you-recommend-using-semicolons-after-every-statement-in-javascript You'll find more of such reviews by searching the net by keyword "semicolon in JavaScript" (without quotes). Hth, cmiiw
1st May 2019, 12:58 PM
Ipang