Back to Course

Introduction to JavaScript

0% Complete
0/0 Steps
  1. JavaScript: Basics
    9Topics
    |
    10 Quizzes
  2. JavaScript: Variables
    9Topics
    |
    10 Quizzes
  3. JavaScript: Conditional Statements
    10Topics
    |
    11 Quizzes
  4. JavaScript: Functions
    10Topics
    |
    11 Quizzes
  5. JavaScript: Scope
    6Topics
    |
    7 Quizzes
  6. JavaScript: Arrays
    11Topics
    |
    12 Quizzes
  7. JavaScript: Loops
    8Topics
    |
    9 Quizzes
  8. JavaScript: High Order Functions
    3Topics
    |
    3 Quizzes
Lesson 1, Topic 3
In Progress

JavaScript: Comments

Yasin Cakal 26 Oct 2021
Lesson Progress
0% Complete

Programming is usually a collaborative effort. Furthermore, when we return to our own code, sometimes only an hour later, it can soon become difficult to understand. For these reasons, leaving notes in our code for other developers or ourselves is always beneficial.

We can write comments in our JavaScript code that the computer will skip as our program runs. These comments are only for users to read. Comments can be used to describe what the code does, give instructions for developers who will be working with it, or add any other helpful annotations.

In JavaScript, there are two types of code comments:

A single-line comment is denoted by two forward slashes // preceding it and will comment out a single line.

//Print 13 to the console
console.log(13); 

You can also comment after a line of code with a single line comment:

console.log(13);  //Print 13 to the console

A multi-line comment will comment out many lines and is indicated by the characters /* to start the comment and */ to end it.

/*
This is all commented 

none of this is will be printed!

till here
*/
console.log(74);

In the example given above, only the number 74 will be printed on the screen. This syntax can also be used to comment out something in the middle of a line of code:

console.log(/*Ignore the text!*/ 8);  // It will just print 8

Single-line comments are a fantastic way to provide context to your code. When it comes to preventing a block of code from running, multi-line comments are usually the best option. Both types of comments, however, can serve the purpose.

%d bloggers like this: