I am new to React Native and Javascript. Can anyone explain me the process behind this. Why Javascript is considering semicolons optional?
Asked
Active
Viewed 37 times
1 Answers
0
As stated in this article;
The JavaScript parser will automatically add a semicolon when:
• Every next line starts with code that breaks the current one
• when the next line starts with a }, closing the current block
• when the end of the source code file is reached
• when there is a return statement
• when there is a break statement
• when there is a throw statement
• when there is a continue statement

spinfal
- 64
- 6
-
To hammer the point home: The semicolon is not generally optional, it is instead only optional is these specific situations. – Ouroborus Dec 12 '21 at 12:21
-
I can't think of any situations off the top of my head for when semicolons are required, but there are definitely times when it is. I recommend searching and reading more about it so you can see the examples. – spinfal Dec 12 '21 at 18:40
-
Practically, there's only one or two situations that might naturally occur where the semicolon rules can catch you out. I'm not sure how common it is as I usually use semicolons in my own code. It's one of those things that, once you solve it, you know to watch for it, but it's infuriating the first time you do run into it because the code looks fine. – Ouroborus Dec 13 '21 at 01:29
-
Yeah, they can definitely be very annoying! Usually your IDE should catch it though and let you know that it **expected** a semicolon. – spinfal Dec 13 '21 at 02:41