Semicolons

  • 21.1 Yup. eslint: semi

    Why? When JavaScript encounters a line break without a semicolon, it uses a set of rules called Automatic Semicolon Insertion to determine whether or not it should regard that line break as the end of a statement, and (as the name implies) place a semicolon into your code before the line break if it thinks so. ASI contains a few eccentric behaviors, though, and your code will break if JavaScript misinterprets your line break. These rules will become more complicated as new features become a part of JavaScript. Explicitly terminating your statements and configuring your linter to catch missing semicolons will help prevent you from encountering issues.

    1. // bad - raises exception
    2. const luke = {}
    3. const leia = {}
    4. [luke, leia].forEach(jedi => jedi.father = 'vader')
    5. // bad - raises exception
    6. const reaction = "No! That’s impossible!"
    7. (async function meanwhileOnTheFalcon() {
    8. // handle `leia`, `lando`, `chewie`, `r2`, `c3p0`
    9. // ...
    10. }())
    11. // bad - returns `undefined` instead of the value on the next line - always happens when `return` is on a line by itself because of ASI!
    12. function foo() {
    13. return
    14. 'search your feelings, you know it to be foo'
    15. }
    16. // good
    17. const luke = {};
    18. const leia = {};
    19. [luke, leia].forEach((jedi) => {
    20. jedi.father = 'vader';
    21. });
    22. // good
    23. const reaction = "No! That’s impossible!";
    24. (async function meanwhileOnTheFalcon() {
    25. // handle `leia`, `lando`, `chewie`, `r2`, `c3p0`
    26. // ...
    27. }());
    28. // good
    29. function foo() {
    30. return 'search your feelings, you know it to be foo';
    31. }

    Read more.