Suggestion: Use semicolons in the material to preempt foreseeable problems in future codebases #3532
Labels
general
Issue or pull request that is not specific to any particular part of the course material
Problem: Examples in the material rely on automatic semicolon insertion, a convention that can lead to unexpected behavior.
Solution: Use semicolons in the material.
Scenario:
I recently ran into an issue in the wild when using latest Node.js (v21.7.1) and an .mjs file:
Cannot create property '2' on number '4'
The last line is a valid and commonly recommended way to swap elements in an array without temporary variables or bitwise XOR.
The issue arises from a missing semicolon on line 2, resulting in the code being evaluated as:
There are numerous other problematic scenarios resulting from the omission of semicolon, such as:
Prior to running to the array swap issue, I had regarded these errors mostly as theoretical examples, similar to above, with minimal potential to actually manifest in practical production scenarios.
For many people, this course serves as the sole introductory experience to JavaScript. To avoid foreseeable issues in future codebases, I suggest advocating for the less error-prone approach of including semicolons from the get-go. This way, individuals can make their own informed and conscious decision to omit them as they wish, rather than doing so out of habit.
Further motivation: Google Style Guide, Airbnb Style Guide.
The text was updated successfully, but these errors were encountered: