JavaScript Basic Syntax and Comments

Comments help with collaboration, code documentation, and temporarily disabling parts of your code for testing. in JavaScript, the comments divide to two types: single-line comments and multi-line comments.

Comments help with collaboration, code documentation, and temporarily disabling parts of your code for testing. in JavaScript, the comments divide to two types: single-line comments and multi-line comments.

In this lesson, we will learn about the basics of JavaScript syntax and how to effectively use JavaScript comments to make your code easier to understand and maintain. Understanding syntax is key to writing correct and functional code in any programming language.

What is Syntax?

Syntax in programming languages is like grammar and words in our languages. It defines the rules and structure we must follow and obey when writing code. If we don't follow these rules, our code won’t work correctly, and we may get a lot of errors.

JavaScript code is made up of statements and expressions. Statements are blocks of code that perform actions (commands that control the flow), while expressions evaluate to a value (equations and math contribute to taking a decision). Let's see a basic example of JavaScript syntax.

javascript
          let x = 5;

if (x > 2) {
  console.log('x is greater than 2');
}
        

This example contains two statements: the first assigns a value of 5 to the variable x, and the second checks if x is greater than 2. If it is correct, the message 'x is greater than 2' will be printed in the console.

Statements and Expressions

Statements in JavaScript, like let, for, and if, control the flow of the program. The statements usually are followed by semicolons at the end of the line. JavaScript has a great feature called Automatic Semicolon Insertion (ASI) that adds semicolons where needed even if it is not added manually, but it's also still a good habit to include them manually to prevent errors.

On the other hand, expressions return values. For example, 1 + 1 returns 2, and 5 > 2 evaluates to true. Statements can contain expressions, and vice versa. Both play an important role in building JavaScript programs and are the base you use to write your code.

Comments

Comments are used to communicate either with you or with your team members to explain what your code does, making it easier for you and other developers to understand it in the future. JavaScript ignores comments during execution, meaning they don’t affect your code.

Comments help with collaboration, code documentation, and temporarily disabling parts of your code for testing. In JavaScript, the comments are divided into two types:

  • single-line comments
  • multi-line comments

Single-line Comments

Single-line comments are used for brief explanations or notes in your code that will take only one line. They start with two forward slashes //. Anything after these slashes on the same line will be ignored by JavaScript.

javascript
          // This is a single-line comment
console.log('This will run');
let y = 8; // This assigns 8 to the variable y
        

In the example above, only the code after the comment will execute. Single-line comments are useful for explaining short sections of code or adding quick reminders.

Multi-line Comments

Multi-line comments are used when you need to add longer explanations or temporarily disable blocks of code. They start with /* and end with */. Any text between these symbols will be ignored by JavaScript. Also, it can be only one line.

javascript
          /*
  This is a multi-line comment.
  Any code here will be ignored.
*/
console.log('This will run');
        

Multi-line comments are useful when you need to describe larger sections of code or for notes that will take multiple lines. Anyway, it's best to avoid using them for single-line comments.

Comments Best Practices

While comments are helpful, do not overuse them to avoid confusion that can be caused by many comments. Your code should explain itself where possible. Only use comments when necessary to clarify and simplify complex logics, note areas for future improvements, or communicate with team members.

In most code editors like VSCode, you can quickly add comments with shortcuts: Ctrl + / on Windows/Linux or use Cmd + / on Mac. This makes it easy to comment and uncomment code while coding. These shortcuts really save much time.

Conclusion

Understanding JavaScript's basic syntax and the effective use of comments is a nice first step in your learning trip; also, it will help you to create clean, maintainable code snippets. Comments help you and others understand your code better, and following syntax rules ensures that your code works as expected without unwanted errors, which may waste much time.

In our next lesson, we will learn how variables work in JavaScript and how to declare variables using different keywords that JavaScript offers to developers, like:

  • var
  • let
  • const