Skip to content

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?

  1. Explain "Why": Don't explain what the code does (the code does that); explain why you wrote it that way.
  2. Organization: Use them to mark sections of a large file.
  3. Debugging: Temporarily "comment out" code to prevent it from running while testing.
  4. 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.

let lastName, lastname;
lastName = "Doe";
lastname = "Peterson";

3. Whitespace

JavaScript ignores multiple spaces. You can use whitespace to make your code more readable.

let x = 10;
let y=10; // Both are valid

Comments in JavaScript

Single-line Comments

Use //.

// This is a comment
let x = 5; // This is also a comment

Multi-line Comments

Start with /* and end with */.

/*
This code will
print multiple lines
of text.
*/

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.


← Back: Output | Next: Module 2 - Foundations →