JavaScript Comments and Variables

Published by StudyMuch on

StudyMuch.in JavaScript Tutorials

JavaScript Comments and Variables

In this tutorial of  ” JavaScript Comments and Variables “, you will learn Comments and Variables of JavaScript. How to write comments on JavaScript program, and we learn here about Variables of JavaScript, how to make variable and assign the value of variable. We learn here comments and variables with programming examples of all the topic.

JavaScript Comments

The JavaScript comments are used to deliver the right message through text, numeric value and others. It makes programming easier to read and understand. Comments can be used to add notes, suggestions and hints to a JavaScript code.

Types of Comments in JavaScript

Mainly there are two types of comments in JavaScript, that are given below;

  • Single-Line Comments
  • Multi-Line Comments

Now you will read one by one both types of comments and also see the programming code of both comments.

Coding Studymuch.in

 

Single-Line Comments

Single-Line comments are used in JavaScript programming to write single line comment, you can’t write more than one line in single line comment.

To make a single-line comment, add two forward slash
“//” to the beginning of the comment line. Look below the example of code to better understand.

Programming Example of Single-Line Comment;

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible"
content="IE=edge">
    <meta name="viewport"
content="width=device-width, 
initial-scale=1.0">
    <title>Comment</title>
</head>
<body>
  <p id="comment">Welcome 
   to StudyMuch!</p>
  <button type="button" 
   onclick="myfunc()">
   CHANGE</button>

  <script>
  // This program change contents.
    function myfunc(){
    var text = "Hellow Everyone!";
   // P change in "Hello Everyone."
    document.getElementById 
    ("comment").innerHTML=text
        }
  </script>
</body>
</html>

You can see in the above programming code the green color lien is comment, it was started in two forward slash “//”, that is called the single-line comment.

Multi-Line Comment

Multi line comments are used to write more than one-line comments, to make multiples line of comments in JavaScript, use this “/*” in starting of line and “*/” use this in ends of line.

For examples; look below image

JavaScript Comments

 

 

Programming examples of Multi-Line Comments;

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible"
content="IE=edge">
    <meta name="viewport"
content="width=device-width, 
initial-scale=1.0">
    <title>Comment</title>
</head>
<body>

    <p id="comment">Welcome 
     to StudyMuch!</p>
    <button type="button" 
    onclick="myfunc()">
    CHANGE</button>
    <script>

   /* 
    This program  will change the 
    contents. P will change in 
    "Hello Everyone, 
    when you lick the button
   */

    function myfunc(){
      var text = "Hellow Everyone!";
      document.getElementById 
     ("comment").innerHTML=text
        }
    </script>
</body>
</html>

Above you can see in the programming example, all the green line are the multi-line comments. You can also use this for single line comments in JavaScript programming.

 

JavaScript Variables

Variable in JavaScript is simply a name of storage locations, in programming or even in algebra, we use variables to hold the values. In JavaScript, Variable are used to store data. The variable data stored could be a text, a number and both or others.

You can see below simple variables in programming example, we write variable to “var” (short form of variable) in programming code.

Programming example of variable;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
content="IE=edge">
    <meta name="viewport"
content="width=device-width,
 initial-scale=1.0">
    <title>Variable</title>
</head>
<body>
  <script>
      var firstname = "Shubham";
      var lastname = "Verma";
      var age = 20;
      //Printing the all values.
      document.write(firstname
      +""+lastname+"<br>");
      document.write(age);
  </script>
</body>
</html>

Output of Programming;

JavaScript Comments

You can see above in programming example of variable, firstname, lastname and age are variables.

As you can expect:

  • The text “Shubham” is stored/assigned to firstname.
  • The text “Verma” is stored/assigned to lastname.
  • And the number 30 is stored/assigned to age.

Look one more programming example of variable;

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
content="IE=edge">
    <meta name="viewport"
content="width=device-width, 
initial-scale=1.0">
    <title>Variable</title>
</head>
<body>
  <script>
      var x = 50;
      var y = 30;
      var sum = x+y;
      //Printing the sum values.
      document.write(sum);
  </script>
</body>
</html>

The output of this programming example is sum of 50 and 30 is equal to 80, finally 80 is output. You can see in programming we create three variable x, y and sum.

Naming Variable;

JavaScript variables should be identified with unique names. These unique names are called identifiers. Identifiers can be short as single letters such as x, y, a, b, z, s etc. They could also be descriptive like firstname or lastname etc.

Rules of Naming Variables

  • A name should start a letter, an underscore or a doller sign.
  • A name is case-sensitive.
  • A name cannot start with a number.
  • JavaScript reserved keywords like (var, function, if, else, etc.) cannot be used as variable name.

So, in this tutorial “JavaScript Comments and Variables” you learned about JavaScript Comments, types of writing comments and Variables of JavaScript with programming examples. I hope you read this tutorial well and learn new something about JavaScript. If you have any doubt, ask in the comment section. 

Read Also –


7 Comments

zoritoler imol · September 13, 2022 at 7:04 am

There may be noticeably a bundle to learn about this. I assume you made sure good factors in options also.

zoritoler imol · October 17, 2022 at 6:03 pm

I am glad to be one of several visitants on this outstanding site (:, appreciate it for putting up.

showpiece · April 30, 2023 at 3:23 pm

I ⅾon’t even know hoᴡ I ended up here, but I
thoսght this poѕt was good. I dߋ not know who you are bսt definitely you’re going to a famous Ьloggеr
if you are not already 😉 Cheerѕ!

Corrin Wobser · May 13, 2023 at 9:09 pm

We provide you with a table of all the emoticons that can be used on this application, and the meaning of each symbol. Though it might take some initial effort on your part, the skills garnered from regular and strategic use of social media will create a strong foundation to grow your business on ALL levels.

tlovertonet · May 7, 2024 at 9:54 am

I truly appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thanks again

Daftar untuk mendapatkan 100 USDT · July 14, 2024 at 9:29 am

Your article helped me a lot, is there any more related content? Thanks!

smortergiremal · November 5, 2024 at 2:54 am

Perfectly composed subject matter, Really enjoyed reading through.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *