DONATE  
MENU
CSS
MYSQL
AJAX
JSON
HTML
PHP
JAVASCRIPT

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Introduction » JavaScript Comments
Share Tweet WhatsApp

JavaScript Comments

A JavaScript comment is any text, code and yes, just anything that should not be executed. When JavaScript programs and codes are being executed, comments are practically overlooked. EXAMPLE
//alert("Hello World"); This will not execute

/* var x = 5;
alert(x);
All of these will also not be executed */
Why Comments? You may wonder why you may want to write codes or statements that will not be executed but then, the concept is simple. You use comments to describe the complex tasks that you are having or solving in your program. This practice helps you and others, who may come across the codes, in the future to understand what you did. You can also use comments to "comment out" codes you believe are causing errors or codes you don't want to use anymore instead of outrightly deleting them. Must I Write Comments? Comments are not compulsory since they have no effects on your codes but highly recommended based on the above reasons. JavaScript have two(2) ways of writing comments: Single Line Comments We make use of the single line way of commenting when the comment won't span more than just a single line of text in the JavaScript source code (No line break involve). This means that, after the start of a single line comment, all texts or codes existing on the line where and after it was started won't be executed. Syntax We start a single line comment by simply writing two forward slashes (//) and then, the line of comment. EXAMPLE
<script>
//alert(5); this will not be executed

alert(5); //alert(5); this will not be executed
</script>

Copy PRACTICE IT

In most of our examples, we shall be making use of single line comments in explaining such examples. Multiple Line Comments Multiple line comments are the opposite of single line comments. This means, they span more than one line of codes and even to the end of the source codes if not closed. Syntax They are started with a forward slash and an asterisk (/*). They are also ended with an asterisk and a forward slash (*/). EXAMPLE
<script>
/*None of these codes will be executed.
This is because a multi line is in play

var i = 5;

alert(i);

We need to close the comment or it will continue to the end of the codes
</script>

Copy PRACTICE IT

<script>
/*Comment started

alert("Hello World");

End comment
*/

alert(45);
</script>

Copy PRACTICE IT

SUMMARY
  1. JavaScript comments allow us to define codes or write texts that should not be executed when a program is executing.
  2. This helps us in writing comments regarding what is being done in a program.
  3. JavaScript comments can either be single line or multiple line comments.
  4. Single line comments' effects are only on the line they were started and after they have been started.
  5. Single line comments are started with two forward slashes (//).
  6. Multiple line comments span multiple lines and can even affect all the codes in our script if not ended.
  7. Multiple line comments are started with a forward slash and then an asterisk (/*). They are ended with an asterisk and a forward slash (*/).
Take a short quiz on JavaScript Comments.
Ask a question or tell us if JavaScript Comments is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode