Instead, it raises a TypeError: b is not a function exception, because JavaScript tries to interpret it as. Be careful — some people are very opinionated about semicolons. The tool gives us the option not to use it, so we can avoid semicolons if we want. Just make your own decision based on what works for you.
Regardless, we just need to pay a bit of attention, even if most of the time those basic scenarios never show up in your code. Originally published at flaviocopes. If this article was helpful, tweet it. Tweet a thanks. Learn to code for free. Get started. Forum Donate. Shruti Kapoor. Example 1 What do you expect the output of this to be? Render the index page template.
If no separate statement ending character is used, the role is solely left to the newline character. It has divided responsibilities, as it's not only used for representing a statement ending but also for regular formatting.
For example, object literal is best split to multiple lines for improved readability. In that case, newlines are not statement endings but only formatting.
This sends mixed signals to the user, be it unconsciously or not. Does the newline end the statement, or is it just used for grouping whitespace? Since semicolons are optional, the rules with newline characters take higher priority over semicolons. The following will return undefined even if you are using semicolons. It can be counted as one of JavaScript's idiosyncrasies and is best avoided.
Use semicolons for readability but avoid these traps that stem from newline terminated statements. I'd say use them all the time; most code you'll encounter uses them, and consistency is your friend.
They are required by the ECMAscript standard , see section 7. I always promote the use of semi-colons when writing JavaScript. Often the interpreter will be able to infer them for you; but I have yet to see a reason aside from laziness ;- why you would deliberately write your code in a less precise fashion than possible.
To my mind, if the structure of the code is obvious, it will be really clear where the semicolons go, such that you won't even have to think about it after getting in the habit i. It also gets you into the habit of understanding and delimiting statements in your head, so you have a admittedly marginally better understanding of how your code might parse into an AST or similar.
And that's got to be a good thing when debugging syntax errors. If you don't use them and then minify your code you can run into issues with all your code being on a single line and the browser doesn't fully grasp which command ends where.
The basic idea of semicolons is to tell the browser that you have just finished a command. You should use them. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Should I use semicolons in JavaScript?
0コメント