Tuesday , 18 June 2019
Home / Programming / JavaScript / Comments and Variables – JavaScript Tutorial
Including JavaScript
Including JavaScript

Comments and Variables – JavaScript Tutorial

This tutorial is part of the JavaScript tutorial series for beginners. To view the whole list of tutorials available and the order in which you should follow them, then click here.

Something you’ll find yourself using often are comments and variables. Knowing these 2 concepts is absolutely vital to JavaScript and programming in general. Before you get started, you must understand how statements work.


When your code is executed in the browser, any white space, indents, and new lines are all erased. While the browser doesn’t really care or need your formatting, it’s important you format it anyway for readability. In your script.js file, the line that creates the pop up is one statement. To create multiple statements we do this

As you can see, we now have 2 statements. How do you tell the difference? Well, when you’re finished one statement, you use a semi-colon. A semi-colon signifies the end of a statement and tells JavaScript to execute the next statement. Think of it as a period in a sentence. While JavaScript can tell where each statement ends, it’s always good practice to put each statement on a new line like so.


Let”s clear our script of everything and add this code.

Variables are containers that store data. It’s like math class. You can have a variable named x and it can be equal to any number you want. In the case of programming, you can assign any kind of data you want to a variable like strings, booleans and numbers.We haven’t talked about what these are yet, but we will in the next tutorial.

To create a variable, you first use the keyword var. The var keyword is a reserved keyword in JavaScript. This tells JavaScript you want to create a variable. The next part is to name your variable. You can name your variable anything you want. Here are a couple of rules though.

  • The first letter in a variable name must be either a letter, a dollar sign ($), or an underscore ( _ ).
  • The rest of the name can contain numbers.
  • Names are case sensitive. Variables FOO and foo are 2 totally different names in JavaScript.

We create 2 variables named your_message and empty_message. You have the option to assign a value when you create a variable. To assign a value, you use the equal sign (=). This is then followed by the value you want to assign. In our case, we input a message.

We also have another variable that’s completely empty. You can do this if you want and assign a value later. To assign a value later, you can do something like this.

You’ll notice we omit the var keyword. This is because we already created this variable.  You only use the var keyword to create variables. Once created, you can refer to that variable by it’s name. You’re also allowed to change the values of variables.


Let’s finish things off with comments. Comments don’t do much in a program. In fact, they don’t do anything at all. Comments are a way to insert notes or hints about your program. Here’s how you can create them.  Right after our your_message variable, add this bit of code.

You can insert anything in your comments. It can be as ridiculous or as serious as you want. JavaScript will not bother reading any of it. If you want to create a comment, all you have to do is insert 2 forward slashes. You don’t have to create comments after each statement. You can create comments before and after statements like so.

You can also create block comments. You create these when you have a lot to document and want to have it all readable. You do it like so.

You create block comments by using /* and then closing it with */.


While this may not seem special, variables and comments are used in programs everyday. Variables store data and comments allow you to document your code when it becomes too big to manage. In our next tutorial, we take a look at data types. You’ll learn why we put quotes around our sentences and words.

About Jasko Koyn

Check Also


Audio – Advanced JavaScript Tutorial

The video and audio objects both have the same events, properties and methods. So, you …


  1. you may have an important weblog here! would you prefer to make some invite posts on my blog? feefaefeeadf

  2. There is clearly a bundle to realize about this. I suppose you made some good points in features also. ddcedaecedde

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.