thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

01.01 · Comments / Learn · 4 min

Understand comments

One idea at a time: what a comment is, why it helps, and how to write one.

Activity 1 of 6 in Comments

All courses

JavaScript Basics / 01.01

Understand comments

Not started

Explore comments with six visual slides, annotated code, and Mac/Windows shortcuts.

More options

Your lesson progress

Reading & walkthrough: not startedGuided practice: not started

Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

Three comic panels show Rob in a lab coat finding a mysterious note, adding a helpful explanation at his coding desk, and understanding the code.
Rob’s comic strip · Comments

THE VISUAL WALKTHROUGH

Comments, one idea at a time.

01What is a comment?

A note for a person reading your code.

A comment is text inside your program that JavaScript skips. People read the note; the computer runs the instructions around it.

main.jsANNOTATED EXAMPLE
  1. // Keep the first greeting friendly. A note for us. Skipped by JavaScript.
  2. console.log("Hello, Code Zone!"); An instruction. JavaScript runs this.
OUTPUT
Hello, Code Zone!

A comment stays in the code, but it does not appear in the output.

02Why leave one?

Leave the reason. Future you will thank you.

Code shows what happens. A useful comment can explain why you made a choice, leave a reminder, or temporarily turn off an instruction while you investigate.

A less useful note

// Print a message.Repeats what the instruction already says.

A more useful note

// Keep this friendly for brand-new learners.Explains the reason for your choice.
main.jsANNOTATED EXAMPLE
  1. // Keep this friendly for brand-new learners. The reason behind the choice.
  2. console.log("Welcome aboard!");
OUTPUT
Welcome aboard!

You do not need a comment on every line. Explain something that is not already obvious.

03Single-line comments

Two forward slashes. The rest of the line is a note.

Use // before a note. It can have its own line or sit after an instruction. The comment ends when that line ends.

main.jsANNOTATED EXAMPLE
  1. // A note on its own line. The whole line is a comment.
  2. console.log("Hello!"); // An inline note. The instruction before // still runs.
  3. // console.log("DEBUG: checking things"); Commented-out code stays here, but does not run.
OUTPUT
Hello!

The marker must be outside a quoted string. Inside quotes, // is just part of the text.

04Block comments

Open with /*. Close with */.

A block comment is useful when your note needs more room. JavaScript skips everything between the opening and closing markers, even across several lines.

main.jsANNOTATED EXAMPLE
  1. /* Start the comment.
  2. This is the first message a learner sees.
  3. Keep it friendly and short.
  4. */ End the comment.
  5. console.log("Welcome!"); Back to running instructions.
OUTPUT
Welcome!

Always close the block with */. Block comments cannot be nested inside each other.

05The keyboard shortcut

Comment a line with two keys.

In VS Code, put your cursor anywhere on a line, then hold the modifier key and tap /. Select several lines first to toggle comments on the selection.

macOS

Command sits beside the space bar. The / key is beside right Shift.

Windows

Control sits at the bottom left. The / key is beside right Shift.

Hold Command or Control, tap /, then release. You do not need Shift for this shortcut on the layout shown. Click inside the live editor first so it receives the keys.

Press the same shortcut again to uncomment. The / key shown is a common US keyboard layout; other layouts and custom keybindings may differ.

06See what changes

Same instruction. Different result.

Use the button to switch this example between running code and a comment. Notice what changes in the output. Then try it yourself in the live editor.

main.js
                  console.log("Hello, Code Zone!");
                
OUTPUT

Hello, Code Zone!

The instruction is running.

console.log("Hello, Code Zone!"); console.log("DEBUG: checking the welcome message");Try the shortcut in practice

Click inside the JavaScript editor, place the cursor on the debug line, and use your shortcut. Then run the code.

Commenting out a line keeps it available for later. It stops running until you uncomment it.

Write useful JavaScript comments

Work through 4 focused steps. Read the explanation, type each requested change and check it before moving on. Your own code carries forward.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

4 steps · Saved work · Instant feedback

YOUR TURN

Try the examples on this page

Choose Load this code into the editor beside an example, then edit and Run code here. Predict what will print before running it. This is an exploration workspace, so there is no pass/fail challenge. Its saved work stays separate from the next practice activity.

Load an example, edit it and run it. Word wrap is enabled.

Work in VS Code instead

Copy the starter into a file named practice.js in VS Code. If Node.js is installed, open the terminal in that folder and run node practice.js. Compare the output with the task above. Automated challenge checks are available in the browser editor.

Code example
example.jsjavascript · example
// Welcome someone trying their first JavaScript program.
console.log("Hello, Code Zone!");

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Prefer reading? Open the lesson notes

A note the computer ignores

A comment is a note inside your code. JavaScript ignores it when the program runs. You can explain a decision, leave a reminder, or temporarily stop an instruction from running.

Two forward slashes start a single-line comment. In the example below, only the console.log instruction runs. console.log prints a message so you can see what your program is doing.

A note beside a working instruction
example.jsjavascript · example
// Welcome someone trying their first JavaScript program.
console.log("Hello, Code Zone!");

When your note needs more room

A block comment starts with /* and ends with */. It can span several lines. Everything between those markers is ignored.

A comment across several lines
example.jsjavascript · example
/*
  Keep this greeting friendly.
  It is the first message a new learner will see.
*/
console.log("Welcome aboard!");

Explain the reason

A useful comment explains something a reader would not immediately know. “Keep this message friendly for new learners” tells you more than “print a message.”

In VS Code, select a line and use Ctrl + / on Windows or Command + / on a Mac to toggle a line comment. A browser may handle shortcuts differently, so typing // also works.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

Comments explain your choices. Instructions do the work.