Different Ways To Write Comments in JavaScript

Everything you need to know before you code

Fatima Nawaz
Nov 30, 2020 · 5 min read
Image for post
Image for post
Photo by Goran Ivos on Unsplash

Introduction

Comment Syntax

// This is a comment
/* This is
a comment */
// Print "Hello, World!" to the console
console.log("Hello, World!");
// Initialize a function
function alphabetizeOceans() {
// Define oceans variable as a list of strings
const oceans = ["Pacific", "Atlantic", "Indian", "Antarctic", "Arctic"];
// Print alphabetized array to the console
console.log(oceans.sort());
}

Inline Comments

let x = 99;    // assign numerical value to x
let y = x + 2; // assign the sum of x + 2 to y
for (let i = 0; i === 10; i++) // for loop that runs ten times {
// Running this code results in a syntax error
}

Block Comments

/* Initialize and invoke a the greetUser function
to assign user's name to a constant and print out
a greeting. */
function greetUser() {
const name = prompt("What is your name?");
console.log("Hello ," + name + "! How are you?");
}
greetUser();
/**
* Initialize constant with an array of strings.
* Loop through each item in the array and print
* it to the console.
*/
const seaCreatures = ["Shark", "Fish", "Octopus"];for (const seaCreature of seaCreatures) {
console.log(seaCreature);
}

Commenting Out Code for Testing

// Function to add two numbers
function addTwoNumbers(x, y) {
let sum = x + y;
return sum;
}
// Function to multiply two numbers
function multiplyTwoNumbers(x, y) {
let product = x * y;
return product;
}
/* In this example, we're commenting out the addTwoNumbers
function, therefore preventing it from executing. Only the
multiplyTwoNumbers function will run */
// addTwoNumbers(3, 5);
multiplyTwoNumbers(5, 9);

Conclusion

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store