Comments and Syntax & Structure

Learn how to write clean code using JavaScript syntax and document your work with comments.

What is a JavaScript Comment and Syntax?Link to this section

A comment is a piece of text in your code that the computer ignores when running the program, used to explain what the code does. Syntax refers to the set of grammar rules that you must follow so the JavaScript engine can understand and execute your instructions.

The engine runs the variable assignment and completely skips the comment.

  • `let` is the **keyword** declaring a variable
  • `score` is the **identifier** or variable name
  • `10` is the **value** assigned to the variable
  • `//` starts the **comment** explaining the code

Comments are used for human readers to understand the codebase, while correct syntax ensures the computer does not crash with a syntax error.

Why Do We Need Comments and Syntax?Link to this section

Code is read much more often than it is written, making structure and explanatory notes essential for maintenance.

  • Explain complex shop discount calculations to other programmers
  • Temporarily disable a broken login validation line during testing
  • Document the author and creation date of a salary calculation script
  • Organize long blocks of student mark processing into readable sections
  • Maintain strict punctuation rules so the JavaScript parser does not throw errors
  • Leave TODO notes for future updates on product pricing logic
  • Generate automated documentation from structured code blocks

Using comments allows you to leave notes for yourself or your team directly inside the source code.

The comment points out future work without breaking the execution of the print statement.

About JavaScript Syntax and Structure RulesLink to this section

JavaScript code consists of statements, expressions, keywords, and punctuation rules like semicolons and brackets. Comments can span a single line or multiple lines. Understanding statement structure and whitespace management keeps your scripts error-free and easy to read.

Syntax ElementSymbolPurpose
Single-line comment//Ignores everything to the right on that line
Multi-line comment/* ... */Ignores text spread across multiple lines
Statement terminator;Marks the end of a JavaScript statement
Code block container{ ... }Groups multiple statements together

Multi-line comments are wrapped between forward slash asterisk and asterisk forward slash.

Types of Comments and Syntax StructuresLink to this section

1. Single-Line CommentsLink to this section

Single-line comments start with two forward slashes and run until the end of the line. They are ideal for quick notes, variable descriptions, or temporarily disabling a single command.

The double slash makes the rest of the line a comment.

Commenting out a line prevents it from executing.

tip

Place single-line comments above the line of code they describe rather than squeezed on the far right if they are long.

2. Multi-Line CommentsLink to this section

Multi-line comments begin with a forward slash and an asterisk and end with an asterisk and a forward slash. They are used for longer explanations, copyright notices, or commenting out large blocks of experimental code.

Everything between the opening and closing markers is ignored by the engine.

warning

You cannot nest multi-line comments inside another multi-line comment, as the first closing asterisk forward slash will prematurely end the comment block.

3. Statements and SemicolonsLink to this section

A statement is an instruction given to the computer. JavaScript uses semicolons to separate statements, though automatic semicolon insertion helps if you omit them.

Semicolons clearly mark where individual instructions end.

tip

Always make a habit of adding semicolons at the end of statements to avoid unexpected line-break issues with automatic semicolon insertion.

4. Blocks and Scope BracesLink to this section

Curly braces group multiple statements together into a single block. They define where variables live and control the body of loops and conditional statements.

The braces group the statements that run when the condition is true.

note

Opening and closing braces must always be balanced. Missing a closing brace causes an immediate syntax error.

5. Case Sensitivity in IdentifiersLink to this section

JavaScript is strictly case-sensitive. Variable names, function names, and keywords must have matching capitalization.

student and Student are treated as completely different variables.

warning

Writing keywords like let as Let or console as Console will cause a reference or syntax error.

6. Whitespace and FormattingLink to this section

Spaces, tabs, and line breaks are called whitespace. JavaScript ignores extra whitespace, allowing you to format code neatly for readability.

Line breaks and spaces do not affect execution as long as the syntax remains valid.

tip

Use consistent indentation of 2 or 4 spaces to keep nested blocks readable.

Comments in Inline DebuggingLink to this section

Comments are heavily used during development to toggle lines of code on and off without deleting them, preserving experimental code while tracking down bugs.

Disabling a line lets you test the base calculation without the discount rule.

tip

Use keyboard shortcuts in your code editor to quickly comment or uncomment selected lines.

Common Syntax ErrorsLink to this section

Small typos in syntax lead to program crashes. Recognizing common mistakes saves troubleshooting time.

Unclosed string literal causes a syntax crash.

warning

Forgetting to close quotation marks or missing a parenthesis or curly brace will prevent your entire script from running.

Complete Coding ExampleLink to this section

This complete script combines multi-line headers, single-line notes, structured statements, and clear mathematical expressions.

A well-commented script makes business logic clear to anyone reviewing the code later.

Important NotesLink to this section

  • Comments are completely ignored by the JavaScript engine during execution.
  • Single-line comments start with double forward slashes and cover the rest of the line.
  • Multi-line comments are enclosed between forward slash asterisk and asterisk forward slash.
  • JavaScript is strictly case-sensitive, meaning variable names like total and Total are distinct.
  • Semicolons mark the end of statements, though JavaScript often inserts them automatically.
  • Curly braces group multiple statements into a single executable block.
  • Extra whitespace and line breaks are ignored by the parser but vital for human readability.
  • You cannot nest multi-line comments inside one another.
  • Commenting out code is a standard technique for debugging and testing.
  • Syntax errors stop code execution before any instructions run.

SummaryLink to this section

Mastering comments and syntax is the foundation of writing clean, maintainable JavaScript code. Comments help humans document logic and debug problems, while proper syntax ensures the computer executes your program without errors. Clean formatting and adherence to grammar rules set the stage for all future programming tasks.

Check Your UnderstandingLink to this section

Javascript Comments and Syntax & Structure quiz
Question 1 of 6

Which symbol is used to start a single-line comment in JavaScript?

Practice ChallengesLink to this section

1. Comment Your Variables

easy

Create two variables representing item price and quantity. Add a single-line comment above each variable explaining its purpose, then log the product of both.

Expected output

150

Hint: Use // before your explanation text above the variable declarations.

2. Documenting a Sale Script

easy

Write a multi-line comment at the top of your file with your name and assignment title. Below it, create a price variable and apply a 10 percent discount calculation.

Expected output

90

Hint: Wrap your header notes between /* and */.

3. Debugging with Comments

medium

You are given a code snippet where a faulty line multiplies the score by zero. Use a single-line comment to disable the faulty line and add a working line that adds 10 points instead, starting from an initial score of 50.

Expected output

60

Hint: Comment out the zero multiplication line and write a new addition statement below it.

4. Case Sensitivity Check

medium

Declare a variable named itemTotal with a value of 200. Try to log a variable named ItemTotal (capital I) and observe or handle the case sensitivity rule correctly by logging the correct variable.

Expected output

200

Hint: Remember JavaScript distinguishes between lowercase and uppercase characters.

5. Complex Bill Calculator with Structure

hard

Build a structured billing script with a multi-line comment header, comments explaining tax and tip calculations, clear statement separation with semicolons, and final console output for a 200 dollar meal.

Expected output

250

Hint: Calculate tax at 10 percent and tip at 15 percent, adding them to the base bill.

Rate this content

Sign in to rate

Discussion

Sign in to join the discussion