Warning: include(nectar/meta/meta-config.php) [function.include]: failed to open stream: No such file or directory in /home/content/18/9019618/html/v2014/wp-content/themes/salient/functions.php on line 4650

Warning: include() [function.include]: Failed opening 'nectar/meta/meta-config.php' for inclusion (include_path='.:/usr/local/php5/lib/php') in /home/content/18/9019618/html/v2014/wp-content/themes/salient/functions.php on line 4650
Beginner's Guide To JavaScript Variables And Comments - ...

Beginner’s Guide To JavaScript Variables And Comments – Part 2

By May 13, 2015 Blog No Comments
Beginner's Guide To JavaScript Variables And Comments - ...

In our last post we outlined some of the JavaScript essentials, and for part two in this series, we’ll dig a little deeper into JavaScript variables and comments.

What Are JavaScript Variables?

JavaScript variables are, simply put, containers for storing data values. Last time, we defined JavaScript as a dynamic programming language, which means you don’t have to specify the type of data it contains, just the name of the variable. This also means you can use a variable to store one type of data, and change the data type later on in the same variable. Once assigned, you can refer to the data contained inside the variable simply by using the name of the variable.

Declaring JavaScript Variables

Before using a variable, you need to declare it first. JavaScript variables are declared using the var keyword and a unique name/identifier. The names can contain letters, numbers, underscores, and dollar signs, and only numbers can’t be used as the first character of the name. Another thing you need to remember is that you cannot use spaces when naming JavaScript variables.

Beginner's Guide To JavaScript Variables And Comments - ...Buy Theme Open Demo

Keep in mind that JavaScript is case sensitive, which can be the cause of major problems down the road, especially because the errors can be hard to spot. In order to avoid case sensitivity errors, it’s a good idea to stick with one naming convention. For example, camelCase is a naming convention where the first letter of each word is capitalized, except the first letter.

var sum = 5;
var totalAmount = 5;

If you don’t assign the value of a variable, it will automatically become a global variable, which you should try to avoid whenever you can. Global JavaScript variables can be accessed and updated by any other JavaScript code running on the website, leading to errors and bugs that are almost impossible to spot.

Comments In JavaScript

If you have any experience in HTML and CSS, you should already be familiar with the concept of comments. Comments enable you to add text to your code that the browser and its JavaScript engine will completely ignore, and they can contain all characters. They can be incredibly useful if you’re working in a team with other developers, as they allow you to describe the code and even categorize it.

Beginner's Guide To JavaScript Variables And Comments - ...Buy Theme Open Demo

There are two types of JavaScript comments – single line and multiline comments. Single line comments are created using double forward slashes //. To create multiline comments, you’ll use a forward slash followed by a star to open it, and a star followed by a forward slash to close it.


By now, you should know how to link JavaScript to an HTML document, what JavaScript variables are and how to use them, and how to write comments. Let’s take a look at this piece of code, for example, and then try to crack it together:

var greetingMessage = "Welcome to AvaThemes!";
alert (greetingMessage);
//You can replace our message inside the quotes with your own

The first line declares the variable and assigns it a value, while the second line creates a pop-up alert with the message we defined in the first line. The third line is a single line comment. Go ahead, try it yourself! Pressing Control + Shift + J on Windows computers, or Command + Option + J on Macs will open up the console that enables you to see any bugs in the code. If you’ve done everything like we showed you, there shouldn’t be any alerts in the console.

Next time, we’ll take a closer look at operators and objects, as well as some basic functions. Stay tuned!

Leave a Reply