Syntax & Comments¶
What are Comments?¶
Comments are notes written by the programmer to explain what the code is doing. They are ignored by the computer and do not affect how the program runs.
Why use Comments?¶
- Explain "Why": Don't explain what the code does (the code does that); explain why you wrote it that way.
- Organization: Use them to mark sections of a large file.
- Debugging: Temporarily "comment out" code to prevent it from running while testing.
- Collaboration: Help other developers understand your logic.
Best Practice
Write clear, concise comments. If your code is so complex it needs a paragraph of explanation, consider refactoring the code to be simpler!
JavaScript Syntax¶
JavaScript syntax is the set of rules that defines how JavaScript programs are constructed.
1. Identifiers (Names)¶
Identifiers are names for variables, functions, and keywords.
- Must begin with a letter, $, or _.
- Subsequent characters can be letters, digits, underscores, or dollar signs.
- Camel Case: By convention, JS uses camelCase (e.g., firstName, userScore).
2. Case Sensitivity¶
JavaScript is Case Sensitive. myVariable and myvariable are two different variables.
3. Whitespace¶
JavaScript ignores multiple spaces. You can use whitespace to make your code more readable.
Comments in JavaScript¶
Single-line Comments¶
Use //.
Multi-line Comments¶
Start with /* and end with */.
Semicolons
In JavaScript, semicolons are used to separate statements. While often optional (the browser will guess where they belong), it is a professional standard to always include them to prevent rare but confusing bugs.